简体   繁体   中英

UnboundLocalError: local variable 'totalp' referenced before assignment

I'm having an error where totalp does not seem to be computing. The user is supposed to enter a number for the item they want until the press 5 that stops the order, then the total price is calculated for the entries but I can't figure out how to do that, the error being part of the problem.

This is the error I'm getting:

UnboundLocalError: local variable 'totalp referenced before assignment`

Here is my code:

print ("""
MENU ITEMS \t \t  PRICES
1. Pizza \t \t  $7.29 sm  $12.39 lg

TOPPINGS
2. Green Peppers \t  $1.50
3. Mushrooms \t \t  $1.00
4. Pepperoni \t \t  $2.00
5. Complete my order
    """)

name = input("What is your name? ")

p_number = "How many pizzas would you like to purchase? "
t_number = "How many different toppings would you like to add? "

totalp = 0
totalt = 0

def choose_menu():

    print("")
    menu = int(input("What would you like to order? ")) 
    if menu == 0:
        choose_top()
    if menu == 1: 
        p_size = input("What size of pizza would you like? \n (s for small, l for large): ")
        if p_size == "s":
            sprice = 7.29
            totalp += sprice
            print("")
            print("You purchased a small pizza.")
        elif p_size == "l":
            lprice = 12.39
            totalp += lprice
            print("")
            print("You purchased a large pizza.")
        else:
            print("")
            print("Invalid entry, please reenter.")
            choose_menu()
        choose_top()
        choose_menu()
        display_receipt()
    elif menu == 5:
        print("Nothing was purchased.")
    else:
        print("Invalid entry, please reenter.")
        choose_menu()


def choose_top():

    print("")
    topping_choice = int(input("What toppings would you like to add? "))

    if topping_choice == 0:
        top = "No toppings were added."
        display_receipt()
    elif topping_choice == 2:
        top = "Green peppers added."
        price2 = 1.50
        totalt += price2
        choose_top()
    elif topping_choice == 3:
        top = "Mushrooms added."
        price3 = 1.00
        totalt += price3
        choose_top()
    elif topping_choice == 4:
        top = "Pepperonis added."
        price4 = 2.00
        totalt += price4
        choose_top()
    elif topping_choice == 5:
        print("Order has been confirmed.")
        display_receipt()
    else:
        print("Invalid entry, please reenter.")
        choose_top()
    print(top)

if menu == 1:
    p_total = p_number * totalp
    t_total = t_number * totalt
    b_tax_total = p_total + t_total
    tax = int(.0825)
    sales_tax = b_tax_total * tax    
    total_price = b_tax_total + sales_tax
    def display_receipt():

print("")
print("CUSTOMER RECEIPT:")
print("Customer name:", name)
print("The total number of pizzas ordered:", p_number)
print("The total number of toppings ordered:", t_number)
print("Total price before tax:")
print(format(b_tax_total, '.2f'))
print("Sales tax:")
print(format(sales_tax, '.2f'))
print("The total amount due:")
print(format(total_price, '.2f'))   


choose_menu()

I think you have messed up your code indents upon copying them over. For example, display_receipt() seems to not have a body as it is right now. Please fix. Also the "if menu == 1:" line seems to be out of place.

Assuming your indentation is good. The error shown above is due to the fact that totalp has been assigned outside the scope of the function in which it is referenced. So use (and, if you need to, read about) the "global" keyword. Basically, if you modify your choose_menu() function to to be like,

def choose_menu():
    global totalp
    print("")
    # code continues as shown above

you will get the behavior you desire. You will probably have to do the same thing for totalt.

It is worth mentioning that using global variables for trivial cases usually isn't the best method. Try passing in the value if you can, or use types that allow for referencing in such a case (for example lists).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM