This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Dominic Henry
over 9 years

tip calculatior

I like to play around when I learn and code things my way so that it can give the same result as in the tutorial.

But I noticed a problem, when I code my way for the tip calculator in the activity of getting the total, the console says am wrong. And I don’t know why.

My Code :

meal = 44.50 tax = 0.0675 tip = 0.15

meal = meal + meal * tax

total = (meal*2) * tip

print(“%.2f” % total)

Code that is suggested :

meal = 44.50 tax = 0.0675 tip = 0.15

meal = meal + meal * tax

total = meal + meal * tip

print(“%.2f” % total)

So can some tell me why I would be getting an error because based on the math I know…both statements are pretty much the same.

Answer 53cfa50f282ae380b9001ab0

0 votes

Permalink

when you post code, highlight it and press ctrl-k or the {} button in the editing panel multiplication is evaluated before addition, both in ‘regular’ math and in python, you changed the order that it is evaluated with the parenthesis

points
Submitted by Jonatan
over 9 years

3 comments

Dominic Henry over 9 years

yes but the thing is multiplication and addition are commutative…so it doesn’t matter the order in which you work them….remember multiplication is repeated addition. So still don’t get why.

Dominic Henry over 9 years

hmmmm…Thank you….I mixed around my math terms and thought wrong….I looked it over and your right thanks.