Math operation priority in python


#1



I can not figure out when it says assign a variable to for instance itself + itself * tax in Reassign in a Single Line Exercise and I assign 2*itself*tax instead, it makes an error because it seems they equal different values.  
meal = 44.50
tax = 0.0675
tip = 0.15

meal = meal*2*tax
print(meal)

Error: Oops, try again. It looks like meal is 6.0075 instead of 47.50375.
just curious to know more about example like this and math operation priority in python! :slight_smile:


#2

Meal * tax = tip

Tip + meal = total

Should help you out a little.


#3

This is because in you code, you just simply computed how much much tax 2 orders of the meal adds to the original price.

If you want to compute tax + meal, instead of this code:
meal = meal*2*tax

Your code should be like this, in order to compute the total of 2 orders + tax:
meal = meal + ((meal * 2) * tax)

The exercise told you that the meal should total to 47.50375 because in the original exercise, it is only asking for one order of the meal. Like this:

meal = meal + (meal * tax)`

Slowing it down, it looks like this. Remember that the expression on the right always get evaluated first before it assign the final computation to the variable.

 meal = 44.50 + (44.50 * 0.0675)
 meal = 44.50 + 3.00375
 meal =  47.50375

I hope that helped!


#4

Thanks that helped a lot