It is always good to have names. This makes comprehending much more easier for humans (computer doesn't care :-).
So you first iteration could become (EDIT: rates are changed compared to your example):
Simple enough. Second iteration requires that tip would be calculated on amount of meal with tax amount. So it would be:
So you first iteration could become (EDIT: rates are changed compared to your example):
>>> meal = 8 >>> tax_rate = 0.15 >>> tip_rate = 0.13 >>> first = meal + (meal * tax_rate) + (meal * tip_rate) >>> print(first) 10.239999999999998
Simple enough. Second iteration requires that tip would be calculated on amount of meal with tax amount. So it would be:
>>> second = meal + (meal * tax_rate) + ((meal + (meal * tax_rate)) * tip_rate) >>> print(second) 10.395999999999999As you can observe that this is quite complicated and we calculated
meal * tax_rate
twice. Modern Python enables us to use assignment expression to avoid this. We calculate tax amount and give it a name and then use this name in next calcuation. So it can be written:>>> second = meal + (tax_amount := meal * tax_rate) + ((meal + tax_amount) * tip_rate) >>> print(second) 10.395999999999999And... Please use Python code tags!
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.