Meal = meal + meal * tip?


#1


https://www.codecademy.com/courses/python-beginner-GB6hM/0/4?curriculum_id=4f89dab3d788890003000096


Can someone explain how this works? I don't seem to understand what is happening here. We are taking the original value of meal (44.50) and reassigning it to meal (44.50) + meal(44.50) * tip. So technically the meal will will be $89 plus the tip?
Shouldn't the code be something like meal = meal * tip?


Replace this line with your code.


#2

Once we make an assignment, the variable takes that value.

a = a + a * b

The expression on the right side is evaluated, then assigned to the variable on the left. The old value is replaced.

As math goes, precedence, or order of operations apply.

multiplication
addition

So the first operation is, a * b. The second operation is a plus the outcome of the first operation.

Now getting a little more technical, the right side expression is a binomial, and we know that multiplication is distributed over all terms in a polynomial, which in reverse, lets us factor out the a

a * (1 + b)

which means the lesson expression can be simplified to,

meal = meal * (1 + tax)

The general rule of thought is to ignore the left side until all the operations are complete on the right side. If we were to create a new variable (so we could keep track of the price) it would take the result of the expression. But if we don't need to keep track, then it is better to re-use variables than create new ones, which is the case here.

Compare your expression to the one above. As it is calculated for this exercise, the tip is calculated on the tax, also, so,

meal = meal * (1 + tax)

meal = meal * (1 + tip)

where both tax and tip are decimal fractions, 0.0675 and, 0.15.

Note the important role that the 1 plays. Multiplying by 1 (unity) has no effect other than to preserve the original value. If we calculated meal * tip we would end up with only the tip. Adding 1 lets us increase the meal cost to the new value.


#3

i passed the exercise by following the instructions even though I was not happy, then I came here to see if I found why the code is different from the one I tough was the correct one

I tried to follow mtf instructions but doesn't make any sense, my idea is below and give me a different total:

meal = 44.50

6.75%

rest_tax = 0.0675

15%

tip = 0.15

totaltax = meal * rest_tax
totaltaxandmeal = totaltax + meal
totaltip = meal * tip
meal_aftertax_aftertip = meal + totaltax + totaltip

print("%.2f" % totaltax)
print("%.2f" % totaltaxandmeal)
print("%.2f" % totaltip)
print("%.2f" % meal_aftertax_aftertip)

this prints a total of 54.18 not 54.63 like mtf code

but my meal is also 47.50 like mtf code


#4

I spent several hours (broken up), trying to figure this one out and realized I was overthinking it.

Instead of literally following the instructions, I was replacing

meal = meal + meal * tip

with the numeric values instead of using the variables, which are already assigned.


#6

Recall that in the lesson version, we are paying a tip on the tax portion, as well. Hence the slightly greater amount.

meal *= 1 + tax + tip            # tip on meal only

versus

meal *= (1 + tax) * (1 + tip)    # tip on meal and tax

In numeric terms...

x *= 1.2175

versus

x *= 1.2276

#7

Got it! Thanks, need to apply the 15% after adding the meal and tax.
My noob code seems to do the same, but written in a different way.


#8

Yes, your code uses the first approach; the lesson uses the second one.


#10

Why must you use meal twice
I know the first meal is the variable
why must you add meal + meal


#11

By order of operation, the addition takes place after the multiplication.

meal + meal * tax
 ||    ----------
 ||    1 * 0.0675
  1  +   0.0675

#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.