简体   繁体   English

为什么我一直收到此 python 错误 cost is not defined?

[英]why do i keep getting this python error cost is not defined?

i wrote this code i get an error say cost is not defined我写了这段代码我得到一个错误说成本没有定义

print("============================================================\n"
      "                 Welcome to Pizza Store                              \n"
      "============================================================\n")
def welcomescreen():
    print("1) Menu for the Pizza")
    print("2) Order the Pizza")
    print("3) Exit the program")
    ch = input("Select from one of the above: ")
    return ch
def main():
    choice = welcomescreen()
    while choice != '3':
            # based on user choice add appropriate method
        if choice == '1':
            print("1. Pepperoni               9 AED\n2. Margherita              12 AED\n3. Vegetarian              15 AED\n4. Neapolitan              21 AED")
            ask = input("Do you want to go back to the main menu? yes/no : \n").lower()
            if ask == "yes":
                main()
            elif ask == "no":
                break
        elif choice == '2':
            n=int(input("Enter the number of pizzas to be ordered: "))
            kind=input("Enter the kind of Pizza: ")
            size=input("Enter the size of Pizza\n(Large (50 AED),Medium (40 AED), Small (30 AED) : ")
            if(size == "Large"):
                cost_size=n*50
            elif(size == "Medium"):
                cost_size=n*40
            elif(size == "Small"):
                cost_size = n*30
            if(kind == "Pepperoni"):
              cost= n*10
              pizza = 'Pepperoni'
            elif(kind== "Margherita" ):
                cost= n*15
                pizza = "Margherita"
            elif(kind == "Vegetarian" ):
                cost= n*20
                pizza = "Vegetarian"
            elif(kind== "Neapolitan"):
                cost= n*18
                pizza = "Neapolitan"
            d=input("Enter toppings: \n").split(" ")
            extra=0
            if(len(d)>3):
                 extra= n*3*(len(d)-3)
#final Bill
            print("---------------------Your BILL-----------------------\n")
            print("The Pizza kind :", kind)
            print("The size :", size)
            print("Number of pizzas :  x", n)
            print("Extra toppings :")
            for i in d:
                print(i,end=" ")
            print("\n")
            print("==========Breakdown of bill========== \n")
            print("Bill for pizza         : ", cost)
            print("Bill for size         : ",cost_size)
            print("Bill for extra toppings: ",extra)
            print("Total Bill             : ",cost+cost_size+extra)

        else:
            print("Invalid choice. Try again.")
        
        choice = welcomescreen()

    print("Thank you! Have a nice day :)")
main()
 

the error i get is:我得到的错误是:


============================================================ Welcome to Pizza Store ================================================ ========== 欢迎来到披萨店

  1. Menu for the Pizza披萨菜单
  2. Order the Pizza订购披萨
  3. Exit the program Select from one of the above: 2 Enter the number of pizzas to be ordered: 1 Enter the kind of Pizza: pepperoni Enter the size of Pizza (Large (50 AED),Medium (40 AED), Small (30 AED): large Enter toppings: pepperoni ---------------------Your BILL-----------------------从上述其中一项退出程序 Select: 2 输入要订购的比萨饼数量: 1 输入比萨饼的种类:意大利辣香肠 输入比萨饼的大小(大(50 迪拉姆)、中(40 迪拉姆)、小(30 迪拉姆) ): large Enter toppings: pepperoni ----------------------Your BILL-------------------- ---

The Pizza kind: pepperoni The size: large Number of pizzas: x 1 Extra toppings: pepperoni披萨种类:意大利辣香肠尺寸:大披萨数量:x 1 额外配料:意大利辣香肠

==========Breakdown of bill========== ==========账单明细==========


Exception has occurred: UnboundLocalError
cannot access local variable 'cost' where it is not associated with a value
  File "C:\Users\mandoof1\Downloads\Pizza part A.py", line 57, in main
    print("Bill for pizza         : ", cost)
  File "C:\Users\mandoof1\Downloads\Pizza part A.py", line 68, in <module>
    main()

what is supposed to happen is take the cost based on your input and give you the bill应该发生的是根据您的输入计算成本并给您账单

cost is only defined if kind is one of the three strings you check against in your if/elifs.只有当kind是您在 if/elifs 中检查的三个字符串之一时,才会定义cost Specifically, I think whats happening is that you're putting in 'pepperoni' but you really want to put in 'Pepperoni' (notice the capitalization)具体来说,我认为发生的事情是你输入了“意大利辣香肠”,但你真的想输入“意大利辣香肠”(注意大写)

To handle this more generally, you probably want to:为了更普遍地处理这个问题,您可能想要:

  1. .lower() the input kind and what you're checking against so you don't have to worry about capitalization .lower()输入kind和你要检查的内容,所以你不必担心大写
  2. Have an else clause when checking kind, so that if the user puts in something unexpected you know about it.在检查种类时有一个else子句,这样如果用户输入了意想不到的东西,你就会知道。
  3. Repeat these two fixes for the size input as well.size输入也重复这两个修复。

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

相关问题 为什么我不断收到名称错误:名称“模型”未定义 - Why do I keep getting Name Error: name 'model' is not defined 为什么我不断收到名称未定义的错误? - Why do I keep getting the name not defined error? 为什么在上述类中定义TwitterClient时,为什么不断收到93行错误,提示未定义TwitterClient? - Why do I keep getting a line 93 error saying TwitterClient not defined when it is defined in a class above? #Python 为什么我不断收到此代码的命名元组属性错误? - #Python Why do I keep getting namedtuple attribute error for this code? 为什么我在 python 中不断收到客户端/证书错误,不和谐? - Why do I keep getting the Client/Certificate error in python, discord? 为什么我不断收到重定向意外错误python? - Why do I keep getting redirection unexpected error python? 为什么我在 python 3.9 中不断收到属性错误 - why do I keep getting Attribute error in python 3.9 为什么我不断收到 Python Turtle 的终结者错误? - Why do I keep getting a Terminator error for Python Turtle? 为什么我在给定的 python 代码中不断出错? - Why do i keep getting error in the given python code? 为什么我的Django项目中总是出现此“名称未定义模型”错误? - Why do I keep getting this “name 'Model' is not defined” error in my Django project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM