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,但它们是定义的。
答案 0 :(得分:2)
代码自上而下线性运行(禁止跳跃,如函数调用和类初始化)。因为您在底部初始化变量,所以到达if-else块时,代码尚未创建值。如果将变量放在首位,则代码将正常运行。