Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NEED HELP NEW TO PYTHON!!
#1
Lightbulb 
Hello! Im new to coding in python I always found coding interesting and wanted to take a dive in it and so far its been understandable but I have come across a problem which is throwing me in a loop. The question is
" Write a program that prints the total bill, including tax and a 15% tip, for a meal that costs $8.00. The tax rate in Ontario is 13%.

The total bill will be the sum of the meal cost, the tax, and the tip.

Calculate the tax on the meal cost only (there should be no tax paid on the tip) and calculate the tip on the meal cost only (there should be no tip paid on the tax).

Don't worry if your answer gives more than two decimal places of accuracy."

Ive come up with this code and the solution:
print(8 + (8 * .13) + (8 * .15))
10.239999999999998

Now the next question is the one throwing me in the loop because the wording is confusing me but I also want to see first hand why the solution i putting is not the answer. Q :
"Now modify your program to be more generous. This time, you should calculate the tip on the meal plus the tax. The tax should still be calculated on the meal alone.

The meal cost is still $8.00 and the tax and tip rates are still 13% and 15%, respectively.

All you need to change is the calculation of the tip.

Don't worry if your answer gives more than two decimal places of accuracy."

My code/solution is:
print((8 + (8 * .13)) + (8 * .15))
10.239999999999998

and im still given wrong?
can someone help me see the solution around this.
Reply
#2
You still need to add the TAX on the TIP.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#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
#4
thank you for the reply! Im starting python which the university of waterloo has "python starting from scratch"course I am taking! just from stating what is what in python itself made everything seem more easier to understand instead of just looking at numbers! If there is a python learning course you recommend to a someone starting out please!
Reply
#5
(Aug-14-2021, 07:09 AM)perfringo Wrote: 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!
Key here was that you were thinking of programming, not thinking about the problem. The problem was clearly stated: compute the tip on the meal plus tax. Once you express that in English (or French), the answer is obvious. Don't get caught in the syntax trap. Work it out in your native language first, then write down how to do it so a seven-year-old could figure it out (well, maybe an 8-year-old). If you can't explain it to a small child, you can't program it (small children are much, much smarter than computers). That's one of the secrets of successful programming.

I was working with my intern a couple days ago. I wanted to leave him with a project to do a particular computation. We wrote about 30 lines of comments explaining what the algorithm was and how it had to work. His programming will be trivial now. Because everything is written down in simple English (our shared language).
Reply
#6
First, your question about sources for learning Python - there are lots of them. What programming experience do you have already? Know what languages? Do you have a particular learning style?

Second, you will get lots of options in your problem. I agree that you are looking at programming rather than problem, and take it a step farther. To calculate tip, you multiply by 1.15 rather than by 0.15 and adding to the base. Similar for tax. So,

meal = 8
tip = 1.15
tax = 1.13
bill_1 = meal*tip + meal*tax - meal #don't double count meal
bill_2 = meal*tax*tip
print(f'The two calculated bills are {bill_1} and {bill_2}')
Output:
The two calculated bills are 10.239999999999998 and 10.395999999999999
Reply
#7
print(round((8 + (8 * .13)) + (8 * .15), 10))
print((8 + (8 * .13)) + (8 * .15))
Output:
10.24 10.239999999999998
Reply


Forum Jump:

User Panel Messages

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