簡體   English   中英

創建變量時出現“無效語法”錯誤

[英]Getting “Invalid Syntax” Error when creating variable

我正在為一項任務創建這個程序。 我正在嘗試創建一個變量來命名一個稍后將在代碼中打印的新變量。

"""
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

我見過其他人遇到與我類似的問題,但我不確定是什么問題。 幫助將不勝感激。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM