Python Forum
wrong values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: wrong values (/thread-10990.html)



wrong values - cybervulcan - Jun-16-2018

I entered code from programming book "Python's Companion" about a bill calculator and although the script worked it returned a value of 111.78 when the book said the returned value should be 7.78 the code is correct exactly as typed when I looked at a variable with the id() function it returned a value that was not set in the variable declaration statement and I am not sure why

meal = 45
sales_tax = 7/100
tip = 20/100
meal += meal + meal * sales_tax
bill = meal + meal * tip
I am using Python 3.5.2 on Linux Mint 18.3 any help is appreciated?


RE: wrong values - Larz60+ - Jun-16-2018

since the meal cost $45 I don't think meal + tax + tip will equal 7.78. That would be a pretty neat trick.
At any rate, this is wrong:
meal += meal + meal * sales_tax
and should be:
meal = meal + meal * sales_tax



RE: wrong values - micseydel - Jun-16-2018

Code aside, a $45 meal after tip (20%) and tax (7%) will not end up being less than $45 (the number you cited as the book saying, 7.78). It would be $57.15 if you tip on the meal amount and $57.78 if you tip on the meal after tax has been added.

I suggest you work out the math separately, then try to code that up independent of the book, if you want to take another look at this problem.

With regard to the book - you're either not following it, or it's a bad book and not worth continuing with.


RE: wrong values - cybervulcan - Jun-17-2018

The change Larz60+ suggested gave a result of gave the result of $57.78 Thanks I am just learning python so I will probably have a lot more questions. If anyone can suggest a good resource with syntax reference I would very much appreciate it prefer Kindle ebook but does not have to be. Thanks again

I understand and will attempt to do this in the future however I am not entirely sure since the book presented the code in the above manner with no other tags no other modules were imported