Doesn't seem right


#1

I programmed the script to "apply the tip to the overall cost of the meal including tax" as it describes in the instructions, but it didn't work. I check these forums and someone posted this script which works fine...

meal = 44.5
tax = .0675 * meal
tip = .15 * meal
total_cost = meal + tax + tip

print total_cost

My question is, this script above just adds all three together, it doesn't apply the tip to the cost of the meal plus tax.
Why does this script work when it doesn't do what they asked? Or am I just mixing numbers up somehow?


#2

This is what I ended up with which allowed me to pass, but I'm still curious why the other script passes while it doesn't apply the tip to the cost of the meal plus tax. It seems like that script just adds the meal plus the tax plus the tip which wasn't what we were asked to do.

meal = 44.5
tax = .0675
tip = .15
mealandtax = meal + (tax * meal)
total = meal + (tip * mealandtax)
print total


#3

I was confused at first but then I thought a bit harder.

meal = 44.50
tax = 0.0675
tip = 0.15

meal = meal + meal * tax

You multiply meal * tax first (I was initially adding meal + meal first). Which gives you 3.00375

Then you add 3.00375 + meal(44.50) and you get 47.50375

total = meal + meal * tip

Same deal here only you're using tip instead of tax.

I think we are thinking of it like this (meal + meal) * tax. To be honest I can't tell you why we didn't add before we multiplied. That parts a little confusing to me still.


#4

meal = 44.50 + (44.50 * .0675)
total = 47.50 + (47.50 * .15)

Does it make more sense now?


#5

PEMDAS

Parenthesis
Exponent
Multiplication/division
addition/subtraction

You do not need the parenthesis around "(tax * meal)" in the equation - > mealandtax = meal + (tax * meal)

However, it does make it easier to read, and may help others understand the math better in the future - just adding information that may help further explain the math :smile:


#6

NOW I get it! :smile:
I completely forgot about the order of operations. That's what happens when one has been out of school for a while. Thank you for clarifying.


#7

I was going to say no onen is thinking like this - but it seems someone was.
However, I don't see how this answers the question alucard had - this just seems to be something you were tripping over, then figured out.


#8

Thanks, I read all this and I get it, so Python (as other codes) make the math using PEMDAS. That's why always we have to search the forum before ask XD


#9

read again about rule of variabels