Why reassign the variable meal instead of creating a new variable, like meal_plus_tip or something like that?


#1

My question is the title of this. I don't understand why we would reassign the variable "meal" with the value of the meal and the tip when we can just create a new (and much less confusing) new variable like "meal_plus_tip" or something like that.

Is there some larger purpose to this? It seems really confusing for no purpose. If the "meal" variable represents the price of the meal minus tax and tip, it seems like you could use:

meal
tax
tip
meal_plus_tip
meal_total

That seems less confusing to me, but I don't really know anything about programming, so maybe there is some reason why the other way is better?

Thanks and I hope to hear back on this topic soon!

Paul


#2

No reason, really. Our first goal is to write a program that works, regardless how clumsy it may start out. Then we work at simplifying and refining, just as we would in mathematics. The best solution in most cases is the simplest one.

Let's say the meal cost is 10$.

meal = 10
tip = 0.15 * meal ==  1.50    # tip is computed before adding tax
meal * 1.0675     == 10.68
===========================
total with tip    == 12.18

There is no one right way to arrive at a correct answer. However we do it is up to us.