Python Meal Calculator - declaring "meal" twice?


#1

On the Python meal calculator why does “meal” have to be declared twice?

meal = meal + meal * tax
print(meal)

Why is it not meal = meal * tax? If the meal was $44.50 then meal + meal * tax sounds like $44.50 + $44.50 x tax. Just want to be sure I understand the logic. Thanks.

This was my finished code.

meal = 44.50
tax = 6.75/100

I added this so I could track

meal_tax = meal * tax
print(meal_tax)

tip = 15.0/100

I added this so I could track

meal_tip = meal * tip
print(meal_tip)

meal = meal + meal * tax
print(meal)

total = meal + meal * tip
print(total)


#2

The reason why you write meal = meal + meal * tax is because you need to add the tax to the meal and then overwrite the variable stored at meal to the new total value of the meal. If you were to write meal = meal * tax, that would calculate just the tax alone and overwrite the variable with just the tax. You could alternatively write meal += meal * tax which is what I think would make more sense logically to you given your question.


#3

both are possible, but you have to be careful then:

$44.50 * 1.0675

or:

$44.50 + $44.50 * 0.0675

remember that 44.50 * 0.0675 only gives the tax ($2.5 or something). Which is why you have to add 44.50 again. or use 1.0675


#4

Ah okay. Once you convert it all to numbers it makes sense. Thanks guys.


#5

I think it was just the wording of “tax” that threw me off. The variable had not actually already calculated the tax but was the decimalization of the taxable amount. Hence I calculated it separate as variable meal_tax. Thus meal = meal + meal_tax would have been correct. Alrighty, identified the error in my thought process. Moving on. :slight_smile:


#6

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