Python Forum
Why is ordertotal returning zero in my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is ordertotal returning zero in my code?
#1
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()
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code is returning the incorrect values. syntax error 007sonic 6 1,135 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  Is the following code returning a generator expression? quazirfan 8 1,533 Apr-11-2023, 11:44 PM
Last Post: quazirfan
  LDAP code to query for host not returning data burvil 2 3,690 Oct-17-2018, 10:03 PM
Last Post: burvil

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020