[Tutor] How to correct decimal addition.
wesley chun
wescpy at gmail.com
Sat Jan 25 01:34:52 CET 2014
hi leon,
you made a good start and ran into something that i know doesn't seem right
to you. however, before we attack the issue, there are some basic problems
with the code you need to correct first. below are a few explanations and
perhaps workarounds:
1. you're passing in price *and* asking the user for it... pick one, not
both
2. i'll assume you did *not* mean to pass it in for the rest of this
response
3. the == should be a single = otherwise you should get a NameErrorexception
4. the return price + .09 should be indented on the next line
5. the operation price + .09 should fail with a TypeError exception
6. the value .09 is incorrect for the solution you're describing...
choose another
7. assuming you fix all of the problems above and run it, you'll still
get the output you stated
8. however, if you print the value, it'll show correctly
9. if you use Python 2.7.x+ or 3.1.x+, it'll show correctly
10. reference link 1 to review:
http://docs.python.org/tutorial/floatingpoint
11. reference link 2 to review: http://bugs.python.org/issue1580
hope this helps!
--wesley
On Fri, Jan 24, 2014 at 9:57 AM, Leon S <imtherealleon at gmail.com> wrote:
> Here is what I'm trying to do, accept a price of gas, but I want to add
> the .009 to the price, so that people do not have to type the full amount.
> Example, 3.49 /gallon would return 3.499 /gallon.
>
> This is what I have tried and the results of it.
>
> def gas_price(price):
> price == raw_input("What is the price of gas?") return price + .09 3.49=> 3.4899999999999998
>
>
> It reduces the number and then adds many decimal points after.
>
>
> Thanks for any help, I am sure this is an easy one for someone.
>
>
--
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
+wesley chun <http://google.com/+WesleyChun> : wescpy at gmail :
@wescpy<http://twitter.com/wescpy>
Python training & consulting : http://CyberwebConsulting.com
"Core Python" books : http://CorePython.com
Python blog: http://wescpy.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140124/05906879/attachment-0001.html>
More information about the Tutor
mailing list