创建变量时出现“无效语法”错误

时间:2021-03-16 19:24:52

标签: python syntax-error

我正在为一项任务创建这个程序。我正在尝试创建一个变量来命名一个稍后将在代码中打印的新变量。

"""
This program asks the user for three ingredients,
three amounts, and a number of servings, and
determines how much of each ingredient is needed
to serve the specified number of servings.
"""

# Write program here...
print("Welcome to Salad Calculator!")
firstfood = input("What is the first ingredient?: ")
firstounce = float(input("How many ounces of this ingredient do you need?: ")
secounce = input("What is the second ingredient?: ")
secounce = float(input("How many ounces of this ingredient do you need?: ")
thirfood = input("What is the third ingredient?: ")
thirounce = float(input("How many ounces of this ingredient do you need?: ")
servings = int(input("How many servings do you need to make? (Enter a whole number): ")
print(" ")

print("You will need " + str(firstounce * servings) + " ounces of " + firstfood)
print("You will need " + str(secounce * servings) + " ounces of " + secfood)
print("You will need " + str(thirounce * servings) + " ounces of " + thirfood)

跑步时我得到:

File "main.py", line 12
    secounce = input("What is the second ingredient?: ")
           ^
SyntaxError: invalid syntax

我见过其他人遇到与我类似的问题,但我不确定是什么问题。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

firstounce 缺少第二个右括号。以下是固定代码

"""
This program asks the user for three ingredients,
three amounts, and a number of servings, and
determines how much of each ingredient is needed
to serve the specified number of servings.
"""

# Write program here...
print("Welcome to Salad Calculator!")
firstfood = input("What is the first ingredient?: ")
firstounce = float(input("How many ounces of this ingredient do you need?: "))
secfood = input("What is the second ingredient?: ")
secounce = float(input("How many ounces of this ingredient do you need?: "))
thirfood = input("What is the third ingredient?: ")
thirounce = float(input("How many ounces of this ingredient do you need?: "))
servings = int(input("How many servings do you need to make? (Enter a whole number): "))
print(" ")

print("You will need " + str(firstounce * servings) + " ounces of " + firstfood)
print("You will need " + str(secounce * servings) + " ounces of " + secfood)
print("You will need " + str(thirounce * servings) + " ounces of " + thirfood)