The most common answer for the 3.5.9 Recipe CodeHS is:
“””
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…
ia = str(input(“Enter ingredient 1: “))
ib = float(input(“Ounces of “+ str(ia)))
ja = str(input(“Enter ingredient 2: “))
jb = float(input(“Ounces of “+ str(ia)))
ka = str(input(“Enter ingredient 3: “))
kb = float(input(“Ounces of “+str(ia)))
numserv = int(input(“Number of servings: “))
print(“Total ounces of ” + ia + “: ” + str((ibnumserv))) print(“Total ounces of ” + ja + “: ” + str((jbnumserv)))
print(“Total ounces of ” + ka + “: ” + str((kb*numserv)))
This code calculates the total amount of each ingredient needed based on the number of servings. However, there are a few issues and improvements that can be made to the code:
- A typo in this input prompts for the amounts of ingredients 2 and 3. They incorrectly reference
ia
instead ofja
andka
respectively. - The formula for calculating the total amount of each ingredient is not properly enclosed in parentheses.
- For better readability, it’s good practice to use spaces around operators.
Here is the corrected version of this program:
# Ask user for ingredients and their amounts
ia = input("Enter ingredient 1: ")
ib = float(input("Ounces of " + ia + ": "))
ja = input("Enter ingredient 2: ")
jb = float(input("Ounces of " + ja + ": "))
ka = input("Enter ingredient 3: ")
kb = float(input("Ounces of " + ka + ": "))
# Ask for the number of servings
num_serv = int(input("Number of servings: "))
# Calculate and print the total amount of each ingredient needed
print("Total ounces of " + ia + ": " + str(ib * num_serv))
print("Total ounces of " + ja + ": " + str(jb * num_serv))
print("Total ounces of " + ka + ": " + str(kb * num_serv))
This program will now correctly ask for three ingredients, their amounts in ounces, and the number of servings. It then calculates and displays the total amount of each ingredient needed to serve the specified number of servings.
Leave a comment