Home > database >  I'm doing python course on mooc.fi but im stuck on "Food expidenture"
I'm doing python course on mooc.fi but im stuck on "Food expidenture"

Time:09-18

# Write your solution here
times = int(input("How many times a week do you eat at the student cafeteria? "))
price = float(input("The price of a typical student lunch? "))
groc = float(input("How much money do you spend on groceries in a week? "))

print("Average food expenditure:")
print (f"Daily: {times * price   groc / 7} ")

puts out:

How many times a week do you eat at the student cafeteria? 4
The price of a typical student lunch? 2.5
How much money do you spend on groceries in a week? 28.5
Average food expenditure:
Daily: 14.071428571428571 I need it to be 5.5 not 14.07

Pls can anyone help out must be piece of cake for a pro XD

also need weekly output of 38.5. but i dont know what im doing wrong i think i must prioritize the math but idk how to do it.

CodePudding user response:

You have a precedence problem.

In arithmetic expressions, multiplication and division happen before addition. So this:

times * price   groc / 7

divides groc by 7 and then adds it to the result of multiplying times by price. You want instead to divide the whole value tiems * price groc by 7, which means you need parentheses:

(times * price   groc) / 7

CodePudding user response:

well for the daily output just use parentheses. like simple math, ((times*price) groc)/7 and I'm pretty sure this will do.

as for the weekly if I'm getting this right, just do this,

weekly = (daily * 7) again, if you are doing this directly use parentheses.

  • Related