I need some help with some python code. I am working on a college project with pearson revel python and I am having a hard time trying to find the problem with my code. Here is the code I have been using:
user_cookies_input = float(input('Enter number of cookies: '))cups_of_sugar = 1.5cups_of_butter = 1.0cups_of_flour = 2.75number_of_cookies = 48sugar_output = (user_cookies_input * cups_of_sugar) / number_of_cookiesbutter_output = (user_cookies_input * cups_of_butter) / number_of_cookiesflour_output = (user_cookies_input * cups_of_flour) / number_of_cookiesprint("You need" + int(sugar_output, '.2f') + "cups of sugar," + int(butter_output, '.2f') +"cups of butter, and" + int(flour_output, '.2f') + "cups of flour.")
if anyone reads this, please help me out as soon as you can.Thank you.
Edit: The question given to this project is this below
A cookie recipe calls for the following ingredients:• 1.5 cups of sugar• 1 cup of butter• 2.75 cups of flourThe recipe produces 48 cookies with this amount of ingredients. Write a program that asks the user how many cookies they want to make and then displays the number of cups of each ingredient needed for the specified number of cookies in the following format:
You need 5 cups of sugar, 3 cups of butter, and 7 cups of flour.
Best Answer
First of all to concatinate strings with integers/floats you need to convert the integer/float to a string. Second off all, this '.2f'
from what I know only work's with f-strings
. Anyways, I think a good habit is to use f-strings
, they are extremely efficient and simple.
print(f"You need {sugar_output:.2f} cups of sugar, {butter_output:.2f} cups of butter, and {flour_output:.2f} cups of flour.")
code:
user_cookies_input = float(input('Enter number of cookies: '))cups_of_sugar = 1.5cups_of_butter = 1.0cups_of_flour = 2.75number_of_cookies = 48sugar_output = (user_cookies_input * cups_of_sugar) / number_of_cookiesbutter_output = (user_cookies_input * cups_of_butter) / number_of_cookiesflour_output = (user_cookies_input * cups_of_flour) / number_of_cookiesprint("You need " + str(round(sugar_output,2)) + " cups of sugar, " + str(round(butter_output,2)) + " cups of butter, and " + str(round(flour_output,2)) + " cups of flour.")
result:
Enter number of cookies: 5You need 0.16 cups of sugar, 0.1 cups of butter, and 0.29 cups of flour.
This will be marked as correct for Pearsons mylab
cookies=input("Enter number of cookies:")x=(int(cookies))s=(x/48)*1.5b=(x/48)*1f=(x/48)*2.75print("You need "+str(s)+" cups of sugar, "+str(b)+" cups of butter," " and " +str(f)+" cups of flour.")
have no spacing after (cookies:)
in line 1 and change str(round(flour_output,2))
to str(round(flour_output,3))
user_cookies_input = float(input('Enter number of cookies:'))cups_of_sugar = 1.5cups_of_butter = 1.0cups_of_flour = 2.75number_of_cookies = 48sugar_output = (user_cookies_input * cups_of_sugar) / number_of_cookiesbutter_output = (user_cookies_input * cups_of_butter) / number_of_cookiesflour_output = (user_cookies_input * cups_of_flour) / number_of_cookiesprint("You need " + str(round(sugar_output,2)) + " cups of sugar, " + str(round(butter_output,2)) + " cups of butter, and " + str(round(flour_output,3)) + " cups of flour.")
Output:
Enter number of cookies:24You need 0.75 cups of sugar, 0.5 cups of butter, and 1.375 cups of flour