Closed Thread
Results 1 to 10 of 10

Thread: Help with simple python coding

  1. #1
    koshia is offline Newbie
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Help with simple python coding

    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: Help with simple python coding

    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.

  4. #3
    koshia is offline Newbie
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Re: Help with simple python coding

    Quote Originally Posted by Hignar View Post
    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.
    Thank you. I have to head out for work as well. I will take a look at the code again tonight and revise it.

  5. #4
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: Help with simple python coding

    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:

    Code:
        endProgram = "yes"
    should be like this

    Code:
        endProgram = "no"
    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.


    I also changed this:


    Code:
            getOrder()
            thisOrderTotal = calcThisOrder(thisOrder, thisOrder2, thisOrder3)
            orderTotal = calcOrder(thisOrderTotal)
            printOrder(OrderTotal)
    to this:


    Code:
            OrderTotal = calcOrder(getOrder())
            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.



    This

    Code:
        endOrder = "no"
        while endOrder == "yes":
    should be changed to this:


    Code:
        endOrder = "yes"
        
        thisOrder1 = 0
        thisOrder2 = 0
        thisOrder3 = 0
        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.


    You can't use the same variable for this:

    Code:
            thisOrder = input("Enter your choice: ")
    and this:

    Code:
                item1 = input("How many Yum Yum Burgers would you like to order?")
                item1 = float(item1)
                thisOrder = item1 * 0.99
    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)
                thisOrder1 = item1 * 0.99
    and in the first one to be:

    Code:
            selectOrder = input("Enter your choice: ")



    This:

    Code:
                item1 = input("How many Yum Yum Burgers would you like to order?")
                item1 = float(item1)
                thisOrder = item1 * 0.99
                return thisOrder
    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)
                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

  6. #5
    koshia is offline Newbie
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Re: Help with simple python coding

    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:
    Code:
                item1 = input("How many Yum Yum Burgers would you like to order?")
                item1 = float(item1)
    so it is like this:

    Code:
                item1 = float(input("How many Yum Yum Burgers would you like to order?"))
    Would it be the same and is that best practice?

    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 .

  7. #6
    manux's Avatar
    manux is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    234
    Blog Entries
    1
    Rep Power
    14

    Re: Help with simple python coding

    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())

  8. #7
    koshia is offline Newbie
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Re: Help with simple python coding

    Quote Originally Posted by manux View Post
    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 if I wanted to.. I could do:
    Code:
    endProgram = yes.lower() in ['Yes' , 'yes' , 'y' , 'Y' , 'YeS' , 'yEs' , 'yES' ]
    Also, what about accumulator. Whenever I want to do the same process (reference multiple value for a sum) use =+ instead?


    Thanks manux.

  9. #8
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: Help with simple python coding

    So you just change

    Code:
            endOrder = raw_input("Would you like to order more items? Enter yes or no.")
    to

    Code:
            endOrder = raw_input("Would you like to order more items? Enter yes or no.").lower()
    and this:


    Code:
            endProgram = raw_input("Would you like to stop the program? Please enter yes or no. ")
    to this:

    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 -=.

  10. #9
    koshia is offline Newbie
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Re: Help with simple python coding

    Sorry for the confusion vswe. I was trying to ask if the += was an accumulator, but it turns out.. the

    Code:
    return thisOrder1 + thisOrder2 + thisOrder3
    was the accumulation.

    I understand the whole program now though. Thanks a lot you guys/gals. I'll see you next program that is more complex .

  11. #10
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: Help with simple python coding

    Looking forward to that. Glad to be to any help

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How do I make a loop for my simple PHP coding?
    By project168 in forum PHP Development
    Replies: 4
    Last Post: 03-28-2011, 04:14 PM
  2. Need Help with Python Coding - I'm a New Guy
    By AndersonSingh in forum Python
    Replies: 1
    Last Post: 11-13-2010, 07:03 PM
  3. [Charny Coding - Javascript] Simple Clock [Video]
    By Charny in forum Video Tutorials
    Replies: 0
    Last Post: 07-08-2010, 11:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts