【发布时间】:2018-08-03 03:12:09
【问题描述】:
我是python的新手,所以请原谅可能存在的知识不足。
我正在制作一个简单的早餐菜单项列表。当然有很多处理方式,我选择了这种方式。一种“初学者”的方式。 下面是我正在使用的代码。当它到达 Toppings 部分时,接收输入。它转到 print() 方法并由于未定义“膳食”而崩溃。我通过 Python 运行调试器,这有助于我了解发生了什么。在接收到输入值并通过 if elif else 语句运行后,为什么值“保持不变”足够长以返回所有 3 个选择的整个值?在此先感谢....
我还在学习;最终会有其他语句提供其他选项,如果我选择选项之外的东西,就会发生一个函数。 如果我只选择 2 或 1 个选项,将显示输出。目前,这给了我一个问题。 Please Please Please Pleaseeeeeee 我需要帮助,同学们到老师们。
#正在使用的实际代码
print("1. Eggs")
print("2. Pancakes")
print("3. Waffles")
print("4. OatMeal")
MainChoice = int(input("Choose a breakfast item #: "))
if(MainChoice == 1):
Meal = "Eggs"
print("You've Choosen eggs")
elif (MainChoice == 2):
Meal = "Pancakes"
print("You've Choosen pankcakes")
elif (MainChoice == 3):
Meal = "Waffles"
print("You've Choosen waffles")
else:
print("You've Choosen Oatmeal")
if (MainChoice <= 4):
print("1. Wheat Toast")
print("2. Sour Dough")
print("3. Rye Toast")
print("4. White Bread")
Bread = int(input("Choose a type of bread: "))
elif (Bread == 1):
print("You chose " + Meal + " with wheat toast.")
elif (Bread == 2):
print("You chose " + Meal + " with sour dough.")
elif (Bread == 3):
print("You chose " + Meal + " with rye toast.")
elif (Bread == 4):
print("You chose " + Meal + " with pancakes.")
else:
print("We have eggs, but not that kind of bread.")
if ((MainChoice >= 1) or (MainChoice <= 3)):
print("1. Syrup")
print("2. Strawberries")
print("3. Powdered Sugar")
Topping = int(input("Choose a topping: "))
if (Topping == 1):
print ("You chose " + Meal + " with and syrup and Bread.")
elif (Topping == 2):
print ("You chose " + Meal + " with and strawberries Bread.")
elif (Topping == 3):
print ("You chose " + Meal + " with and powdered sugar Bread.")
else:
print ("We have " + Meal + ", but not that topping Bread.")
if (MainChoice == 4):
print("You chose oatmeal.")
else:
print("Thank You for coming by and Eatting with us!")
错误消息 如果选择 燕麦片 和其他物品: 选择燕麦项目后,选择其他项目时, 出现错误消息
通过 Python 输出
- 鸡蛋
- 煎饼
- 华夫饼
- 燕麦片 选择早餐项目#: 4 您选择了燕麦片
- 小麦吐司
- 酸面团
- 黑麦吐司
- 白面包 选择一种面包:1 回溯(最近一次通话最后): 文件“/Users/(admin_name/Desktop/FOLDER/Breakfast-Menu.py”,第 25 行,在 print("你选择了 " + Meal + " 配小麦吐司。") NameError: name 'Meal' 没有定义
选择一切时的输出 #1
- 鸡蛋
- 煎饼
- 华夫饼
- 燕麦片 选择早餐项目#: 1 你选择了鸡蛋
- 小麦吐司
- 酸面团
- 黑麦吐司
- 白面包 选择一种面包:1 您选择了鸡蛋配小麦吐司。
- 糖浆
- 草莓
- 糖粉 选择浇头:1 你选择了鸡蛋和糖浆和面包。 感谢您光临并与我们一起用餐!
另外我如何让面包显示与选择Meal,我知道Meal值是用户输入,在第1和第2个选择之后,抓住第3个,怎么样存储到 Meal 中的所有 3 个选择的值以在最后输出选择?如果我问得对的话。
【问题讨论】:
-
您在这里似乎有多个问题,我不确定我是否理解所有这些问题。但是:(1)您需要在第一个 else 下有一个
Meal = "Oatmeal",就像其他 if/elif 分支下的所有Meal = "…"语句一样。 (2) 所有三个选项的值没有存储到Meal;这就是为什么你必须显式打印Meal + "…"。如果要将其存储到Meal,则必须执行Meal = Meal + " with wheat bread"之类的操作。 -
我理解这个概念并将在编码中实现它。谢谢
标签: python