[Tutor] Help...
Dave Angel
davea at ieee.org
Tue Jul 28 12:04:12 CEST 2009
Ryan V wrote:
> For this source code, i am getting some errors, and not sure how to fix it,
> as i am returning all the values. it is telling me that there is no value
> for the variables i am returning. any help is greatly appreciated!
> Source
> #main function
> def main():
> print 'The menu is:'
> print 'Yum Yum Burger for $0.99'
> print 'Grease Yum Fries for $0.79'
> print 'Soda Yum For $1.09'
> mealOrder = input_meal()
> burgerPrice = input_burger()
> friesPrice = input_fries()
> sodaPrice = input_soda()
> mealEnd = input_mealEnd()
> calc_total(burgerPrice, friesPrice, sodaPrice)
> # print_info(total)
>
> def input_meal():
> print 'If you want the Yum Yum Burger please press 1'
> print 'If you want the Grease Yum Fries please press 2'
> print 'If you want the Soda Yum please press 3'
> print 'If you entered no instead of yes just hit 4'
> mealOrder = input('Enter Now - ')
> if mealOrder == '1' :
> input_burger()
> elif mealOrder == '2' :
> input_fries()
> elif mealOrder == '3' :
> input_soda()
> elif mealOrder == '4' :
> calc_total(burgerPrice, friesPrice, sodaPrice)
>
> def input_burger():
> amountBurger = input('How many burgers would you like?')
> burgerPrice = amountBurger * 0.99
> input_mealEnd()
> return burgerPrice
>
> def input_fries():
> amountFries = input('How many Fries would you like?')
> friesPrice = amountFries * 0.79
> input_mealEnd()
> return friesPrice
>
> def input_soda():
> amountSoda = input('How many sodas would you like?')
> sodaPrice = amountSoda * 1.09
> input_mealEnd()
> return sodaPrice
>
> def input_mealEnd():
> mealEnd = raw_input('Would you like to end your order? (Enter yes or
> no)')
> if mealEnd == 'yes' :
> calc_total(burgerPrice, friesPrice, sodaPrice)
> elif mealEnd == 'no' :
> input_meal()
>
> #Calculation of meal cost
> def calc_total(burgerPrice, friesPrice, sodaPrice):
> totalFood = burgerPrice + friesPrice + sodaPrice
> totalTax = totalFood * .06
> total = totalTax + totalFood
> print 'The total price for food is $', totalFood
> print 'The Tax is $', totalTax
> print 'The total is $', total
> #Displays total, and what you ordered
> #def print_info(total):
> # print 'The meal price is $', total
>
> #call main function
> main()
> and here is the output i am getting
>
> The menu is:
> Yum Yum Burger for $0.99
> Grease Yum Fries for $0.79
> Soda Yum For $1.09
> If you want the Yum Yum Burger please press 1
> If you want the Grease Yum Fries please press 2
> If you want the Soda Yum please press 3
> If you entered no instead of yes just hit 4
> Enter Now - 1
> How many burgers would you like?2
> Would you like to end your order? (Enter yes or no)no
> If you want the Yum Yum Burger please press 1
> If you want the Grease Yum Fries please press 2
> If you want the Soda Yum please press 3
> If you entered no instead of yes just hit 4
> Enter Now - 2
> How many Fries would you like?2
> Would you like to end your order? (Enter yes or no)no
> If you want the Yum Yum Burger please press 1
> If you want the Grease Yum Fries please press 2
> If you want the Soda Yum please press 3
> If you entered no instead of yes just hit 4
> Enter Now - 3
> How many sodas would you like?2
> Would you like to end your order? (Enter yes or no)yes
> *Traceback (most recent call last):
> File "...", line 74, in <module>
> main()
> File "...", line 16, in main
> sodaPrice = input_soda()
> File "...", line 51, in input_soda
> input_mealEnd()
> File "...", line 57, in input_mealEnd
> calc_total(burgerPrice, friesPrice, sodaPrice)
> NameError: global name 'burgerPrice' is not defined*
>
>
Please tell us - is this your first programming language, or are you
comfortable with another, and you need to see how Python differs from
it? I suspect it's your first.
The main problem is that you're trying to use a function meal_end() to
do flow control that needs to be in main(). Each time the user says
"no" to "Would you like to end your order" you don't loop back to the
same place, you call a new copy of input_meal(). (That's called
recursion, and it's very useful, but not here). But you never return to
the first copy of input_meal(). So you end up with several copies of
that function. And only when all of them return (which won't happen)
would you get back to main(). You need a loop (while or for), not
recursion.
Second problem is that you're calling functions that return values, and
only some of the time recording those actual values anywhere. When/if
input_meal() calls input_burger(), it doesn't save the result anywhere.
So if it did return, main() still couldn't get the value, it's already lost.
Third problem is that you have function input_mealEnd() that defines
only one variable, but then expect three others to somehow have useful
values.
Fourth is that you don't have a good way to terminate the program.
Where do you expect to go after printing the current order? You might
expect to start a new order, or something else, but that would require
that you be back at main(), or returning from main().
The best answer to the loop problem is to make a loop either in main()
or in input_meal(). Currently it's not clear which one you had intended
to be the loop, since they both expect to ask for all the items. Since
input_meal() never returns, main() never gets its chance to ask, nor to
save the results. But basically, you have to consolidate these two
functions, and make most of the function a while-loop.
The best answer to the problem of the local variables is to define a
class. In fact, all these functions should probably be methods of that
class. Then you'd have an instance of the class for each customer, and
member data (attributes) for each of the things she can order. But I
expect that classes are a later lesson in your course. So you probably
need to arrange that all data is propagated back to your primary loop,
so it's all visible at the same time.
So, to fix the problems:
truncate main() after the call to input_meal(). let input_meal() do all
the work for this customer, and maybe main() will someday ask for next
customer or something like that.
input_meal() needs to be a while loop You need to start by initializing
the three variables burgerPrice, friesPrice, and sodaPrice to 0, so
they'll always have some value. Initlialize meal_order to 0 as well,
and loop while it's not equal to 4.
Now, when input_meal calls each of those functions, you have two
choices, based on the answer to the following question. What do you
want to happen if the customer chooses "burgers" twice, and enters two
different amounts? Does he get both orders, or does the second value
replace the first? That will determine what you do with the return
value of input_burger(), for example.
Now, when the while loop finishes, you can print the totals and return
to main(). You're done with that customer.
DaveA
More information about the Tutor
mailing list