【问题标题】:Python Breakfast Item Menu issuePython早餐项目菜单问题
【发布时间】: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 输出

  1. 鸡蛋
  2. 煎饼
  3. 华夫饼
  4. 燕麦片 选择早餐项目#: 4 您选择了燕麦片
  5. 小麦吐司
  6. 酸面团
  7. 黑麦吐司
  8. 白面包 选择一种面包:1 回溯(最近一次通话最后): 文件“/Users/(admin_name/Desktop/FOLDER/Breakfast-Menu.py”,第 25 行,在 print("你选择了 " + Meal + " 配小麦吐司。") NameError: name 'Meal' 没有定义

选择一切时的输出 #1

  1. 鸡蛋
  2. 煎饼
  3. 华夫饼
  4. 燕麦片 选择早餐项目#: 1 你选择了鸡蛋
  5. 小麦吐司
  6. 酸面团
  7. 黑麦吐司
  8. 白面包 选择一种面包:1 您选择了鸡蛋配小麦吐司。
  9. 糖浆
  10. 草莓
  11. 糖粉 选择浇头: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


【解决方案1】:

看看MainChoice 为 4 时会发生什么 - 仅执行一个条件语句,在本例中是 else 语句。 ifelif 语句下的代码都不会被执行,只有else 下的代码会被执行。因此,在您的程序中(当输入为 4 时)没有定义变量 Meal

您需要在else 语句中添加Meal 变量的定义才能使您的代码工作,这样当输入为4 时,Python 实际上有一个值可用作Meal 变量。

希望这会有所帮助。

【讨论】:

  • 谢谢,我就是这么想的。我不确定在一开始或在你提到的部分定义膳食。我会这样做然后显示结果。非常感谢您的意见和帮助。
【解决方案2】:

当您选择 OatMeal 时,您没有为 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.")

【讨论】:

  • 是的,我正在查看它,因为它处于调试状态。它将被添加并运行以查看结果。将更新。
【解决方案3】:

大多数人已经回答了您关于为什么没有定义“膳食”的直接问题,但我注意到代码中还有一些其他内容。我将做一些假设:

如果没有选择面包,则不能选择面包的配料。在配料选项中,您打印配料在面包上,但在面包中您不允许选择任何面包。

如果选择了面包,则在选择浇头时,应将浇头列为面包上。

如果选择了燕麦片,那么您既不需要面包,也不需要浇头。浇头之前的“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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 2012-06-09
    • 1970-01-01
    相关资源
    最近更新 更多