简体   繁体   中英

Getting “Invalid Syntax” Error when creating variable

I'm creating this program for an assignment. I'm trying to create a variable to name a new variable that'll be printed later in the code.

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

When ran I get:

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

I've seen other people having problems similar to mine, but I'm not sure what's wrong. Help would be much appreciated.

firstounce lacked the second closing parenthesis. the following is the fixed code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM