【问题标题】:Where is the error that I'm not finding in this Python code? [closed]我在此 Python 代码中找不到的错误在哪里? [关闭]
【发布时间】:2021-02-28 03:10:00
【问题描述】:

我找不到错误。我将非常感谢一些帮助!提前谢谢你。

""" 该程序要求用户提供三种成分, 三份量和份数,以及 确定每种成分需要多少 送达指定份数。 """

ingredient_one = input("What is the first ingredient?: ")
ing_one_oz = int(input("How many ounces of this ingredient?: ")

ingredient_two = input("What is the second ingredient?: ")
ing_two_oz = int(input("How many ounces of this ingredient?: ")

ingredient_three = input("What is the third ingredient?: ")
ing_three_oz = int(input("How many ounces of this ingredient?: ")



servings_amt = int(input("How many servings would you like?: ")

ing_one_oz = ing_one_oz * servings_amt
ing_two_oz = ing_two_oz * servings_amt
ing_three_oz = ing_three_oz * servings_amt

print("In order to make " + str(servings_amt) + " servings of your recipe, you will need " + str(ing_one_oz) + " ounces of " /
+ str(ingredient_one) + ", " + str(ing_two_oz) + " ounces of " + str(ingredient_two) + ", and " + str(ing_three_oz) /
+ " ounces of " + str(ingredient_three) + ".")

输出在第 3 行显示语法错误,该错误以变量成分_two 开头。 第 1 行已经通过“正确”,但第 3 行没有,尽管它们实际上是相同的。

【问题讨论】:

  • 您的多行括号不匹配,例如int(input("How many ounces of this ingredient?: ") 缺少右括号。另外,考虑使用f-strings 进行字符串格式化。
  • 您的错误是拼写错误。你在很多地方都错过了第二个)。我建议你使用像 PyCharm 这样的 IDE 来捕捉这些。

标签: python variables input syntax-error


【解决方案1】:

我不是python专家,但是

int(input("How many ounces of this ingredient?: ")

不需要第二个“)”?

【讨论】:

    【解决方案2】:

    您的错误在第 2 行。您需要在 ing_one_oz 末尾多加一个括号

    ing_one_oz = int(input("How many ounces of this ingredient?: ")
    

    Python 对拾取前几行出现的错误很有趣。

    【讨论】:

      【解决方案3】:

      错误位于第 #2、#4、#6、#7 行,即 ing_one_oz、ing_two_oz、ing_three_oz、servings_amt。在您将输入类型转换为“int”的地方关闭括号

      【讨论】: