【问题标题】:why do i keep getting this python error cost is not defined?为什么我不断收到此 python 错误 cost is not defined?
【发布时间】:2022-11-16 02:19:14
【问题描述】:

我写了这段代码我得到一个错误说成本没有定义

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()
 

我得到的错误是:


================================================ ========== 欢迎来到披萨店

  1. 披萨菜单
  2. 订购披萨
  3. 退出程序 从以上选项中选择一项:2 输入要订购的披萨数量:1 输入比萨的种类:意大利辣香肠 输入披萨的大小 (大(50 AED),中(40 AED),小(30 AED):大 输入配料: 意大利辣香肠 ----------------------您的账单------------------------

    披萨种类:意大利辣香肠 尺寸:大 披萨数量:x 1 额外浇头: 意大利辣香肠

    ==========账单明细==========

    
    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()
    

    应该发生的是根据您的输入计算成本并给您账单

【问题讨论】:

标签: python python-3.x


【解决方案1】:

cost 仅当 kind 是您在 if/elifs 中检查的三个字符串之一时才被定义。具体来说,我认为发生的事情是你输入了“意大利辣香肠”,但你真的想输入“意大利辣香肠”(注意大写)

为了更普遍地处理这个问题,您可能想要:

  1. .lower() 输入 kind 以及您要检查的内容,因此您不必担心大写
  2. 在检查种类时有一个else 子句,这样如果用户输入了意想不到的东西,您就会知道。
  3. 也对 size 输入重复这两个修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多