Closed Thread
Results 1 to 4 of 4

Thread: Help with python program

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

    Help with python program

    Code:
    def main():
        burgerCount = 0
        friesTotal = 0
        sodaTotal = 0
        tax = 0
        subTotal = 0
        total = 0
        order = getOrder()
        burgerCount = getBurger()
        subTotal = getSubTotal(burgerCount, friesTotal, sodaTotal)
        tax = getTax(subTotal)
        total = getTotal(subTotal, tax)
    
    
    def getOrder():
        print ('Welcome to Yum Yum Burger.')
        print ('The menu is:')
        print ('Yum Yum Burger are $0.99')
        print ('Greasy Yum Fries are $0.79')
        print ('Soda Yum are $1.09')
        print ('Enter 1 for Yum Yum Burgers.')
        print ('Enter 2 for Greasy Yum Fries.')
        print ('Enter 3 for Soda Yum.')
        order = raw_input('Enter selection now. ')
        if order == '1':
            getBurger()
        elif order == '2':
            getFries()
        elif order == '3':
            getSoda()
        endOrder = raw_input ('Would you like to order anything else? Enter yes or no. ')
        if endOrder == 'no' :
            getSubTotal(burgerCount, friesTotal, sodaTotal)
        elif endOrder == 'yes' :
            getOrder()
        
    def getBurger():
        burgerCount = input('How many Yum Yum Burgers would you like to order? ')
        burgerCount = burgerCount * 0.99
        burgerCount = float(burgerCount)
        return burgerCount
    
    def getFries():
        friesTotal = input('How many Greasy Yum Fries would you like to order? ')
        friesTotal = friesTotal * 0.79
        friesTotal = float(friesTotal)
        return friesTotal
    
    def getSoda():
        sodaTotal = input('How many Soda Yum would you like to order? ')
        sodaTotal = sodaTotal * 1.09
        sodaTotal = float(sodaTotal)
        return sodaTotal
    
    def getSubTotal(burgerCount, friesTotal, sodaTotal):
        subTotal = burgerCount + friesTotal + sodaTotal
        subTotal = float(subTotal)
        return subTotal
    
    def getTax(subTotal):
        tax = subTotal * .06
        tax = float(tax)
        return tax
    
    def getTotal(subTotal, tax):
        total = subTotal + tax
        total = float(total)
        return total
    
    def printInfo():
        print ('The Subtotal for the order is $'), subTotal
        print ('The tax for the order is $'), tax
        print ('The total for the order is $'), total
    
    main()
    I am getting the following error:
    Traceback (most recent call last):
    File "E:\LAB 5.5.py", line 75, in <module>
    main()
    File "E:\LAB 5.5.py", line 8, in main
    order = getOrder()
    File "E:\LAB 5.5.py", line 33, in getOrder
    getSubTotal(burgerCount, friesTotal, sodaTotal)
    NameError: global name 'burgerCount' is not defined
    Last edited by WingedPanther; 11-07-2009 at 12:15 PM. Reason: add code tags (the # button)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Help with python program

    Please post this with [code][/code] tags instead of what you have right now. Python is literally unreadable without the whitespace since the whitespace is what determines when functions, loops, etc. end. Also, what is the problem you're facing?
    Wow I changed my sig!

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

    Re: Help with python program

    Please use code tags when posting code. It makes it much easier to read and you are more likely to get the help you're after.

    This seems to be the same task as another poster was working on. You might want to read this thread for some hints. There's actually a complete solution in that thread, but you would be better off trying to improve your own code as you'll learn more this way.

    The error you are getting is due to variable scope. You haven't defined burgerCount, friesTotal and sodaTotal outside of the the functions getBurgers() etc so main() cannot see them to pass to getSubTotal().

    You need to assign the values returned by getBurgers() to a variable which can then be passed to getSubTotal(). You'll also need to do the same for getFries() and getSoda().

    Where you have
    Code:
    if order == '1':
        getBurger()
    elif order == '2':
        getFries()
    elif order == '3':
        getSoda()
    you need something like
    Code:
    if order == '1':
        burgers += getBurger()
    elif order == '2':
        fries += getFries()
    elif order == '3':
        soda += getSoda()
    you can then pass these variables to getSubTotal()
    Code:
    subTotal = getSubTotal(burgers, fries, soda)
    There are similar errors elsewhere in your code. For more information on variable scope have a look here.
    If there's a new way, I'll be the first in line.

    But, it better work this time.

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

    Re: Help with python program

    I dont think you understood what the return statement really does

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. python script to c++ program
    By thommyy in forum C and C++
    Replies: 4
    Last Post: 10-04-2011, 01:55 PM
  2. Replies: 12
    Last Post: 07-27-2010, 06:27 AM
  3. Replies: 1
    Last Post: 05-09-2010, 06:39 AM
  4. Should i program games in python or in C++?
    By chinog9 in forum Games for Linux/Unix
    Replies: 12
    Last Post: 01-31-2010, 04:09 PM
  5. New To Python- Little Program i made.
    By Aaron.H in forum Python
    Replies: 24
    Last Post: 11-01-2008, 01:16 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