【发布时间】:2018-11-01 02:13:20
【问题描述】:
我是python编程的新手,做了我的第一个基础程序
我必须访问并打印 if 函数中的本地变量,如果我尝试访问它,它显示可能引用了本地变量
这是完整的代码
def main():
print("Please place your order by filling the options")
name = input("What is your name ")
while not name.isalpha():
print("invalid name")
age = input("What is your age ")
if not age.isdigit():
print("Please type in correct form")
if age >= "50":
print("you are not allowed!")
sys.exit()
item_1 = "burger"
item_2 = "pizza"
print("what would you like to order?")
print(item_1 + "\n" + item_2)
order = input()
item_1_large = "large burger"
item_1_small = "small burger"
item_2_large = "large pizza"
item_2_small = "small pizza"
if order == item_1:
print("What would you like to choose?")
print(item_1_large + "\n" + item_1_small)
selection_of_category = input()
elif order == item_2:
print("What would you like to choose?")
print(item_2_large + "\n" + item_2_small)
selection_of_category = input()
print("How many ")
number_of_order = input()
burger_price_large = int(10)
burger_price_small = int(5)
pizza_price_large = int(15)
pizza_price_small = int(8)
if order == item_1_large:
result = burger_price_large * int(number_of_order)
elif order == item_1_small:
result = burger_price_small * int(number_of_order)
elif order == item_2_large:
result = pizza_price_large * int(number_of_order)
elif order == item_2_small:
result = pizza_price_small * int(number_of_order)
if order == item_1:
print("Your Burger Order Has Been Placed")
elif order == item_2:
print("Your Pizza Order Has Been Placed")
else:
print("You have made wrong choice")
print("Dear Mr. " + name, "Your Total Bill is $" + str (result))
while True:
main()
if input("Would you like to order something? (Y/N)").strip().upper() != 'Y':
today = date.today()
print("Thank you for your order")
print(today)
break
我遇到以下错误 print("尊敬的先生" + name, "您的账单总额是 $" + str (result)) UnboundLocalError:赋值前引用了局部变量“结果”
【问题讨论】:
-
在调用
print(...)行之前,代码中没有任何result = ...分配被执行。所以Python不知道如何处理result这个变量,它没有任何价值(不要和None混淆)