Python Forum

Full Version: Why is ordertotal returning zero in my code?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to use a variable ordertotal to compute the total of an order with tax included in the code below. In the last line of my code I am trying to figure out why the ordertotal variable returns a zero value in this print statement: print('The total for your order comes to ${0:.2f} including tax. Have a nice day!'.format(float(ordertotal)))

If i put .format(float(sum(order) + (sum(order) * .043)) it computes correctly I just don't understand why if i use the variable ordertotal
it returns 0.


Here is the entire code below (look at the last line):


appetizers = {'Chicken Wings':8.99, 'Nachos':6.99,'Mozzarella Sticks':5.99,'Pretzels':5.99,'Onion Rings':4.99,'Cheeseburger Sliders':8.99,'Cheese Fries':8.99}

order = []

ordertotal = sum(order) + (sum(order) * .043)


def mainmenu():

    print("\n\tDan's Sports Bar Menu")

    print("\t1. Appetizers")
    print("\t2. Pizza")
    print("\t3. Burgers")
    print("\t4. Sandwiches")
    print("\t5. Desserts")

    food = input('\nPlease select one of the options above to view our menu selections:')

    if food == '1' or food.casefold() == 'appetizers':
        print("\n\t\tAppetizers")
        print("\t1. Chicken Wings $8.99")
        print("\t2. Nachos $6.99")
        print("\t3. Mozzarella Sticks $5.99")
        print("\t4. Pretzels $5.99")
        print("\t5. Onion Rings $4.99")
        print("\t6. Cheeseburger Sliders $8.99")
        print("\t7. Cheese Fries $8.99")
        while True:
            appetizer = input('Enter the menu number to select the appetizer you would like to order. To return to the main menu type main menu.')
            if appetizer == 'main menu':
                    mainmenu()
            elif appetizer == '1':
                print('You have placed on order for chicken wings.')
                order.append(float(appetizers['Chicken Wings']))
                checkout = input('Would you like to order another item on the menu? Type yes or no.')

                if checkout == 'yes' or checkout == 'y':
                    mainmenu()
                else:
                    print('The total for your order comes to ${0:.2f} including tax. Have a nice day!'.format(float(ordertotal)))
                    exit()
You calculate ordertotal on line 5. At that point order is an empty list, so ordertotal gets assigned the value 0. You never assign anything else to ordertotal, so it is still zero at the end of the program. This is not a dynamic thing where the value of ordertotal changes as the values used in the assignment change. It is calculated once, when you make the assignment, and that's it. You need to do that calculation at the end, right before you print the value.

Instead of calling mainmenu recursively every time a choice is made, I would put this all in a while loop. And I would put order and ordertotal in the mainmenu function, to avoid the confusion that global variables often bring.