Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NEED HELP NEW TO PYTHON!!
#3
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):

>>> 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.395999999999999
As 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.395999999999999
And... Please use Python code tags!
RZola likes this post
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.
Reply


Messages In This Thread
NEED HELP NEW TO PYTHON!! - by RZola - Aug-14-2021, 06:01 AM
RE: NEED HELP NEW TO PYTHON!! - by DPaul - Aug-14-2021, 07:02 AM
RE: NEED HELP NEW TO PYTHON!! - by perfringo - Aug-14-2021, 07:09 AM
RE: NEED HELP NEW TO PYTHON!! - by supuflounder - Aug-14-2021, 08:04 PM
RE: NEED HELP NEW TO PYTHON!! - by RZola - Aug-14-2021, 07:53 AM
RE: NEED HELP NEW TO PYTHON!! - by jefsummers - Aug-15-2021, 12:24 PM
RE: NEED HELP NEW TO PYTHON!! - by naughtyCat - Aug-24-2021, 08:20 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020