Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wrong values
#1
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?
Reply
#2
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
Reply
#3
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.
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,976 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  what is wrong. Why python doesn't ad my values ? Bubu93200 8 2,364 Apr-28-2022, 10:53 PM
Last Post: Bubu93200
  python gives wrong string length and wrong character thienson30 2 3,152 Oct-15-2019, 08:54 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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