我是python新手,所以请提前,请原谅可能缺乏的知识。
我正在研究一个简单的早餐菜单项列表。当然,有很多处理方法,我选择了这种方法。一种“初学者”的方式。 下面是我正在使用的代码。当它进入“浇头”部分时,接收输入。转到print()方法,由于未定义“ Meal”而崩溃。我通过Python运行调试器,这有助于我了解发生了什么。在接收到输入值并通过if elif else语句运行之后,为什么值“保持不变”的时间不能足够长以返回所有3个选择的全部值?在此先感谢...。
我还在学习;最终会有其他语句提供其他选项,如果我在选项之外选择其他选项,则会发生功能。 如果仅选择2个或1个选项,则会显示输出。现在,这给了我一个问题。 Please Please Please Pleaseeeeeeeee,我需要帮助,同学们,老师。
已使用#个实际代码
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退出
如果选择了所有#1,则输出
另外,如何通过选择 Meal 使面包显示出来,我理解Meal值是用户输入,在第1和第2个选择之后,抓住第三个,如何存储在膳食中的所有3个选择的值,以最后输出选择?如果我问得正确的话。
答案 0 :(得分:1)
看看MainChoice
为4时会发生什么-仅执行一个条件语句,在这种情况下为else
语句。 if
或elif
语句下的所有代码都不会执行,只有else
下的代码才能执行。这样,在程序中(输入为4时)就没有定义变量Meal
的意义。
您需要在Meal
语句中添加else
变量的定义,以使您的代码起作用,以便在输入为4时,Python实际上具有一个用作{{1 }}变量。
希望这会有所帮助。
答案 1 :(得分:0)
选择燕麦粉时,没有为 Meal 变量设置任何内容。
与设置餐点的1、2、3选项不同。
print("1. Eggs")
print("2. Pancakes")
print("3. Waffles")
print("4. OatMeal")
MainChoice = int(input("Choose a breakfast item #: "))
if(MainChoice == 1):
Meal = "Eggs" # Meal Set!
print("You've Choosen eggs")
elif (MainChoice == 2):
Meal = "Pancakes" # Meal Set!
print("You've Choosen pankcakes")
elif (MainChoice == 3):
Meal = "Waffles" # Meal Set!
print("You've Choosen waffles")
else:
# No Meal set :c
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:
# No Meal called
print("We have eggs, but not that kind of bread.")
答案 2 :(得分:0)
大多数人回答了您为什么未定义“进餐”的直接问题,但是我注意到了代码中的其他内容。我将做一些假设:
如果未选择面包,则无法选择面包的浇头。在浇头的选项中,您打印出浇头在面包上,但是在面包中不允许选择面包。
如果选择了面包,则在选择浇头时,应将浇头列在面包上。
如果选择了燕麦片,那么您既不需要面包,也不需要浇头。浇头前的“ if”语句无论如何都会列出浇头(我想您想要的是“与”,而不是“或”)。但是,无论是否显示浇头,都要求客户选择浇头。我认为这是一个格式问题。
在声明中,“我们有鸡蛋,但没有这种面包。”,我认为应该是“我们有+膳食+”,但没有那种面包。我基于在浇头选择之后发现的类似陈述。
所有这些,我使用面向对象的方法重写了代码。这并不是回答您的问题的主要意义,而是提出一种不同的解决方法。
UNDEFINED = "undefined"
class Selections:
def __init__(self, statement, *args):
self._items = args
self._response = statement
self._defaultResponse = UNDEFINED
def setDefault(self, statement):
self._defaultResponse = statement
def makeSelection(self):
for index, value in enumerate(self._items):
print(str(index + 1) + ". " + value)
selectedMeal = input("Please choose a breakfast item (by number): ") - 1
if selectedMeal > len(self._items):
if (self._defaultResponse != UNDEFINED):
print(self._defaultResponse)
self.last = UNDEFINED
return
selectedMeal = len(self._items) - 1
self._printResponse(self._items[selectedMeal])
self.last = self._items[selectedMeal]
def _printResponse(self, item):
print(self._response.format(item))
mainSelection = Selections("You've Choosen {}",
"Eggs", "Pancakes", "Waffles", "OatMeal")
mainSelection.makeSelection()
if (mainSelection.last != "OatMeal"):
breadSelection = Selections(
"You chose " + mainSelection.last + " with {}.",
"Wheat Toast", "Sour Dough", "Rye Toast", "White Bread")
breadSelection.setDefault(
"We have {}, but not that kind of bread".format(mainSelection.last))
breadSelection.makeSelection()
if (breadSelection.last != UNDEFINED):
toppingSelection = Selections(
"You chose " + mainSelection.last + " with {} on " + breadSelection.last,
"Syrup", "Strawberries", "Powdered Sugar")
toppingSelection.setDefault(
"We have {} and {}, but not that kind of topping".format(
mainSelection.last, breadSelection.last))
toppingSelection.makeSelection()