Hello everyone,
I am very new to programming and taking a class at the moment. I have an assignment due in a couple of days and I've been trying to get this program to work, but to no avail. A friend referred me to this website and I am hoping one of you can help.
We are learning about counters, accumulators with while and if-then-else statements. My program is an ordering menu of 3 choices with the following information:
Code:Lab 5.5 – Programming Challenge 1 – Yum Yum Burger Joint Write the Pseudocode, Flowchart, and Python code for the following programming problem. Write a program that will calculate the cost of purchasing a meal. This program will include decisions and loops. Details of the program are as follows: * Your menu items only include the following food with accompanied price: o Yum Yum Burger = .99 o Grease Yum Fries = .79 o Soda Yum = 1.09 * Allow the user of the program to purchase any quantity of these items on one order. * Allow the user of the program to purchase one or more types of these items on one order. * After the order is placed, calculate total and add a 6% sales tax. * Print to the screen a receipt showing the total purchase price. Your sample output might look as follows: Enter 1 for Yum Yum Burger Enter 2 for Grease Yum Fries Enter 3 for Soda Yum Enter now ->1 Enter the number of burgers you want 3 Do you want to end your order? (Enter yes or no): no Enter 1 for Yum Yum Burger Enter 2 for Grease Yum Fries Enter 3 for Soda Yum Enter now ->3 Enter the number of sodas you want 2 Do you want to end your order? (Enter yes or no): no Enter 1 for Yum Yum Burger Enter 2 for Grease Yum Fries Enter 3 for Soda Yum Enter now ->1 Enter the number of burgers you want 1 Do you want to end your order? (Enter yes or no): no Enter 1 for Yum Yum Burger Enter 2 for Grease Yum Fries Enter 3 for Soda Yum Enter now ->2 Enter the number of fries you want 2 Do you want to end your order? (Enter yes or no): yes The total price is $ 8.1832 Do you want to end program? (Enter no to process a new order): no Enter 1 for Yum Yum Burger Enter 2 for Grease Yum Fries Enter 3 for Soda Yum Enter now ->2 Enter the number of fries you want 2 Do you want to end your order? (Enter yes or no): no Enter 1 for Yum Yum Burger Enter 2 for Grease Yum Fries Enter 3 for Soda Yum Enter now ->3 Enter the number of sodas you want 2 Do you want to end your order? (Enter yes or no): yes The total price is $ 3.9856 Do you want to end program? (Enter no to process a new order): yes
The code is as follow:
Code:# #------------------------------------------------------------------------------- # # Program : The Yum Yum Ordering Program # Unit : 8 # Lab : 5.5 # Programmer : S.Le # # Description: This program will take orders from three choices. Cal- # culates how much the person's order will cost with 6% tax # included. # # # #------------------------------------------------------------------------------- # #This program uses If-Then-Else statements, Whiles, and various functions. #Main program def main(): endProgram = "yes" while endProgram == "no": getOrder() thisOrderTotal = calcThisOrder(thisOrder, thisOrder2, thisOrder3) orderTotal = calcOrder(thisOrderTotal) printOrder(OrderTotal) endProgram = raw_input("Would you like to stop the program? Please enter yes or no. ") #This will get the order from users and calculate how much it costs for the quantity ordered. def getOrder(): endOrder = "no" while endOrder == "yes": print "Enter 1 for Yum Yum Burgers." print "Enter 2 for Grease Yum Fries." print "Enter 3 for Soda Yum." thisOrder = input("Enter your choice: ") if thisOrder == 1: item1 = input("How many Yum Yum Burgers would you like to order?") item1 = float(item1) thisOrder = item1 * 0.99 return thisOrder elif thisOrder == 2: item2 = input("How many Grease Yum Fries would you like to order?") item2 = float(item2) thisOrder2 = item2 * 0.796 return thisOrder2 elif thisOrder == 3: item3 = input("How many Soda Yum would you like to order?") item3 = float(item3) thisOrder3 = item3 * 1.09 return thisOrder3 else: print "Please enter either 1, 2, or 3 for your order choice." endOrder = raw_input("Would you like to order more items? Enter yes or no.") #This will calculate the total of the three types of items def calcThisOrder(thisOrder, thisOrder2, thisOrder3): thisOrderTotal = thisOrder + thisOrder2 + thisOrder3 return thisOrderTotal #This will calculate the total with 6% tax included. def calcOrder(thisOrderTotal): orderTotal = (thisOrderTotal * 0.06) + thisOrderTotal return orderTotal #This will print out the total price of the order and what the person ordered. def printOrder(orderTotal): print "Your total with tax is: $", orderTotal main()
If anyone could give me some suggestions, I would surely appreciate it. Thanks in advance.
Last edited by koshia; 11-01-2009 at 08:27 PM. Reason: Putting in the code & information for program
Your first problem is the loops in both main() and getOrder().
The way you have set them up means that the code within the loop never runs. In main() you have set endProgram as "yes" and then told it to run some code while endProgram == "no". As endProgram is set as "yes" the code in the loop never runs. The loop int getOrder() has the same problem.
With the loops fixed you have some issues with variable scope which I'll have a look at later if no-one else has. I'm afraid I need to start work now though.
If there's a new way, I'll be the first in line.
But, it better work this time.
I did some small edits to your program and I will tell you what were wrong and what I changed, here's the final code that should work:
Code:# #------------------------------------------------------------------------------- # # Program : The Yum Yum Ordering Program # Unit : 8 # Lab : 5.5 # Programmer : S.Le # # Description: This program will take orders from three choices. Cal- # culates how much the person's order will cost with 6% tax # included. # # # #------------------------------------------------------------------------------- # #This program uses If-Then-Else statements, Whiles, and various functions. #Main program def main(): endProgram = "no" while endProgram == "no": OrderTotal = calcOrder(getOrder()) printOrder(OrderTotal) endProgram = raw_input("Would you like to stop the program? Please enter yes or no. ") #This will get the order from users and calculate how much it costs for the quantity ordered. def getOrder(): endOrder = "yes" thisOrder1 = 0 thisOrder2 = 0 thisOrder3 = 0 while endOrder == "yes": print "Enter 1 for Yum Yum Burgers." print "Enter 2 for Grease Yum Fries." print "Enter 3 for Soda Yum." selectOrder = input("Enter your choice: ") if selectOrder == 1: item1 = input("How many Yum Yum Burgers would you like to order?") item1 = float(item1) thisOrder1 += item1 * 0.99 elif selectOrder == 2: item2 = input("How many Grease Yum Fries would you like to order?") item2 = float(item2) thisOrder2 += item2 * 0.796 elif selectOrder == 3: item3 = input("How many Soda Yum would you like to order?") item3 = float(item3) thisOrder3 += item3 * 1.09 else: print "Please enter either 1, 2, or 3 for your order choice." endOrder = raw_input("Would you like to order more items? Enter yes or no.") return thisOrder1 + thisOrder2 + thisOrder3 #This will calculate the total with 6% tax included. def calcOrder(thisOrderTotal): orderTotal = (thisOrderTotal * 0.06) + thisOrderTotal return orderTotal #This will print out the total price of the order and what the person ordered. def printOrder(orderTotal): print "Your total with tax is: $", orderTotal main()
And now to the explanation, just as Hignar pointed out this line:
should be like thisCode:endProgram = "yes"
since the while loop will loop while endProgram = "no" and if endProgram starts as "yes" nothing will happen since the program will end right away.Code:endProgram = "no"
I also changed this:
to this:Code:getOrder() thisOrderTotal = calcThisOrder(thisOrder, thisOrder2, thisOrder3) orderTotal = calcOrder(thisOrderTotal) printOrder(OrderTotal)
Since we want to use the value we get from getOrder we have to put it as the value to calcOrder(I've added calcThisOrder to be a part of getOrder since that will be easier). Also the case is important so I changed orderTotal to OrderTotal to match the other OrderTotal.Code:OrderTotal = calcOrder(getOrder()) printOrder(OrderTotal)
This
should be changed to this:Code:endOrder = "no" while endOrder == "yes":
Same thing as before with the loop and we also want to declare thisOrder1, thisOrder2 and thisOrder3 since it's not sure we will order at least one of each.Code:endOrder = "yes" thisOrder1 = 0 thisOrder2 = 0 thisOrder3 = 0 while endOrder == "yes":
You can't use the same variable for this:
and this:Code:thisOrder = input("Enter your choice: ")
since then your first order will always be overwritten by the choice at the beginning. So therefor I changed it in the last code to be:Code:item1 = input("How many Yum Yum Burgers would you like to order?") item1 = float(item1) thisOrder = item1 * 0.99
and in the first one to be:Code:item1 = input("How many Yum Yum Burgers would you like to order?") item1 = float(item1) thisOrder1 = item1 * 0.99
Code:selectOrder = input("Enter your choice: ")
This:
won't work since when you use return it will end the function and return the value. But we want to see if there should be any other orders, also if the user wants to order some more of the same sort we should add that to the already ordered amount, so therefor it should now look like this:Code:item1 = input("How many Yum Yum Burgers would you like to order?") item1 = float(item1) thisOrder = item1 * 0.99 return thisOrder
Code:item1 = input("How many Yum Yum Burgers would you like to order?") item1 = float(item1) thisOrder1 += item1 * 0.99
This line have one tab to few since it should be inside the loop so it prompt the user each time if it wants to continue:
Code:endOrder = raw_input("Would you like to order more items? Enter yes or no.")
and last we want to return the value, not each time. So therefor I added this line to return the sum of the values:
Code:return thisOrder1 + thisOrder2 + thisOrder3
So there was a few small error but noting big. Hope you understood what you did wrong so you won't do it again![]()
Hey Vswe,
Can't tell you how much you helped me out there, I was freaking out for quite some time. Thanks for explaining it in detail, it really helped and I got what you explained to me. I do have a few other questions regarding Python so I hope you don't mind while I pick your brain here.
Can I modify this:
so it is like this:Code:item1 = input("How many Yum Yum Burgers would you like to order?") item1 = float(item1)
Would it be the same and is that best practice?Code:item1 = float(input("How many Yum Yum Burgers would you like to order?"))
The other question is about accumulator. You used the += and the book I am reading never mentioned anything about accumulator other than that it is a variable that is used to accumulate.... lol, real helpful. It didn't have any code example either, so that's why I was lost.
One last thing and that is outside the scope of my class, but it is for my own interest, but... How can I make it so if a person types "Yes, yes, YEs, etc..." and it will take that regardless. I remember someone from another site stated that I could use string.lower(), but I don't know how that applies if I were to use it in this code.
Really appreciate you guys' help. Hope to return the favor in the future.
you can do item1 = float(input("..."))
but watch out, there isn't any error checking.
As for the lower case string, it really does apply, since entering "Yes" will not be recognized as a "yes"
Remember that x.lower() returns the lower case and does not change x itself.
(x=x.lower())
So you just change
toCode:endOrder = raw_input("Would you like to order more items? Enter yes or no.")
and this:Code:endOrder = raw_input("Would you like to order more items? Enter yes or no.").lower()
to this:Code:endProgram = raw_input("Would you like to stop the program? Please enter yes or no. ")
Code:endProgram = raw_input("Would you like to stop the program? Please enter yes or no. ").lower
I don't really get your last question but if you use for example myVariable += 1 it's the same as myVariable = myVariable + 1 and you can also do the same with *=, /= and -=.
Sorry for the confusion vswe. I was trying to ask if the += was an accumulator, but it turns out.. the
was the accumulation.Code:return thisOrder1 + thisOrder2 + thisOrder3
I understand the whole program now though. Thanks a lot you guys/gals. I'll see you next program that is more complex.
Looking forward to that. Glad to be to any help![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks