【问题标题】:Why do I get "NameError: name 'B'/'C'/'V' is not defined"? [closed]为什么我会收到“NameError: name 'B'/'C'/'V' is not defined”? [关闭]
【发布时间】:2020-04-13 23:13:16
【问题描述】:
Question = input("Welcome to the meal chooser program. The base cost is $9.99. Would you like to choose beef, chicken, or the vegetarian option?:")
if Question.casefold() == "beef":
    print("Thank you. Your total cost is","$",'{:.2f}'.format(B))
elif Question.casefold() == "chicken":
    print("Thank you. Your total cost is","$", '{:.2f}'.format(C))
elif Question.casefold() == "vegetarian":
    print("Thank you. Your total cost is","$", '{:.2f}'.format(V))
B = 9.99*1.02
C = 9.99*1.025
V = 9.99*1.03 

每当我运行此程序并输入鸡肉、牛肉或素食时,它都会提示我一条消息,说明 B、C 和 V 的值未定义,但它们是。

【问题讨论】:

标签: python python-3.x nameerror


【解决方案1】:

代码自上而下线性运行(除了函数调用和类初始化等跳转)。因为您在底部初始化变量,所以当您到达 if-else 块时,您的代码尚未创建值。如果你把你的变量放在第一位,代码就会运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 2021-09-08
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2015-01-20
    • 2021-10-10
    相关资源
    最近更新 更多