简体   繁体   English

UnboundLocalError:分配前已引用局部变量“ totalp”

[英]UnboundLocalError: local variable 'totalp' referenced before assignment

I'm having an error where totalp does not seem to be computing. 我有一个错误,其中totalp似乎没有进行计算。 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. 用户应该输入他们想要的商品的编号,直到按5停止订单为止,然后计算输入的总价,但是我不知道该怎么做,因为错误是问题的一部分。

This is the error I'm getting: 这是我得到的错误:

UnboundLocalError: local variable 'totalp referenced before assignment` UnboundLocalError: local variable 'totalp之前引用了UnboundLocalError: local variable 'totalp

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. 例如,display_receipt()似乎现在没有主体。 Please fix. 请解决。 Also the "if menu == 1:" line seems to be out of place. 另外,“ if menu == 1”行似乎也不合适。

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. 上面显示的错误是由于以下事实导致的:totalp已在引用它的函数范围之外分配。 So use (and, if you need to, read about) the "global" keyword. 因此,请使用(并且,如果需要,请阅读)“ global”关键字。 Basically, if you modify your choose_menu() function to to be like, 基本上,如果您将choose_menu()函数修改为类似,

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). 如果可以,请尝试传递该值,或者在这种情况下使用允许引用的类型(例如列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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