Re: [Tutor] How to correct decimal addition.

2014-01-26 Thread spir

On 01/26/2014 04:22 AM, Keith Winston wrote:

On Sat, Jan 25, 2014 at 5:09 PM, Oscar Benjamin
 wrote:

Perhaps it would be better though to point at this:

round(D('0.123456'), 3)

Decimal('0.123')


I think you are right. I didn't even think of round(). I think we have
confounded two issues in this thread, the internal
representation/accuracy, and the final presentation. They aren't
really the same thing, unless we force them to be (i.e. using ints).


Yes. Side-note below.

In programming, there is a general confusion between ideas, such as -1.23> and their representations, like "-1.23" "minus one dot twenty three", 
"moins un virgule vingt-trois", "-1,23", or varipus possible strings of bits. 
There are outer written or spoken representations (numerals, for numbers) and 
internal representations in memory, which we could call "raw data". Raw data are 
meaningless (and type-less). Obviously, many truely believe there are data with 
meaning or type, that is ideas, *in* a machine; especially many speak and 
obviously think as if there were _numbers_ in a computer; as if a machine had a 
mind and thought... Numbers and other ideas are only in "heads" of psychical 
(mental) living things, as far as we know. there are no numbers, colors, texts, 
characters, weapons... in a machine ;-)


The internal representation is no less a representation and a pure convention. 3 
can be written

vv^^
(with, say, bits 'on' and 'off')
it is a pure conventional representation (albeit simple & logical), and works 
because routines supposed to operate on numbers take this conventional 
representation for granted, both for operations and for input/output conversions 
to/from outer written representations.


In both internal and external representations there may be issues related to the 
base (eg in python we can also write hex or bin numbers). And a major issue, 
that will last as long as CPU's don't process decimal fractionals natively 
(so-to-say), is the clash between our expectations related to outer decimals and 
the inner processing in binary representation.


d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Keith Winston
On Sat, Jan 25, 2014 at 5:09 PM, Oscar Benjamin
 wrote:
> Perhaps it would be better though to point at this:
 round(D('0.123456'), 3)
> Decimal('0.123')

I think you are right. I didn't even think of round(). I think we have
confounded two issues in this thread, the internal
representation/accuracy, and the final presentation. They aren't
really the same thing, unless we force them to be (i.e. using ints).

-- 
Keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread spir

On 01/25/2014 10:38 PM, Keith Winston wrote:

Also, just to be clear: I'd suggest floats because decimal requires
importing a module and using the non-built-in features thereof


The issue is not that much whether it's supported by builtins, in software, but 
by CPU's; which is not the case, so that all computations are made in 
software... there used to be some machines with builtin decimals (internal 
representation), maybe there still are some, but certainly not in mainstream 
computers.


d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread spir

On 01/25/2014 10:19 PM, Keith Winston wrote:

On Sat, Jan 25, 2014 at 3:57 AM, spir  wrote:

Note: AFAIK most financial software use integers for this reason and to
avoid (or control) rounding errors.


I don't think this is true (no flame intended, hopefully you know I'm
forever in your debt Denis): there's a famous scam where insiders at a
major financial institution set the software so it always rounds down,
and the difference was deposited in their account. It was a matter of
fractions of pennies on a per-scam basis. I'm not certain if this ever
actually happened (I thought it did, but Snopes seems agnostic).

http://www.snopes.com/business/bank/salami.asp


There's a similar story in France as well, but it was several decades ago. 
Incendentally, I used to know several people involved in financial software 
companies; they did use integers for all computations. Maybe today they use 
decimals, dunno (since then, decimal libs have become far more famous and used).


d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread spir

On 01/25/2014 10:01 PM, Keith Winston wrote:

On Sat, Jan 25, 2014 at 3:57 AM, spir  wrote:

.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.4898


I think there's an inconsistency in your post that might confuse the
answers. You mention in the lead that you want to add 9 ONE HUNDREDTHS
of a dollar, or tenths of a cent (which is in fact how gas is priced
in the US, and yes it's crazy stupid). However in your example you add
only tenths, but then in the answer you appear to have added
hundredths, which makes me think that you didn't cut & paste, but
rather retyped (and mistyped).

This will make it a little trickier to use Denis' last idea of using
integers, since you'll have to take them out one more order of
magnitude.


I guess this is what I wrote (unit was 1/10 cent), or maybe I misunderstand your 
point.



 If this exercise is later followed by interest
calculations, or anything like that, you might regret limiting your
internal accuracy/representation.

I think that you should probably do your math in floating point (why
get complicated? And you might need the accuracy, for hundredths of
dollars and interest) and then format the output to be what you want.
Watch out for rounding.


p = 3.499
print('{0:.3f}'.format(p))   # format a float with 3 places after the decimal

3.499

p = 3.4994
print('{0:.3f}'.format(p))

3.499

p = 3.499
print('{0:.3f}'.format(p))

3.500


Yes; but this only corrects output, fo the user's comfort. If you need to do 
further computations, then the internal representation must also be right (else 
you'll get at best rounding errors, at worst worse ;-), and this can only be 
ensured by computing on integers or decimals. Typically, using integers, you'll 
choose a unit a few orders of magnitude lower than the most precise numbers 
possibly involved in computations (eg, a tax rate of 0.1234567 ;-).


d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Oscar Benjamin
On 25 January 2014 21:38, Keith Winston  wrote:
>
> Also, just to be clear: I'd suggest floats because decimal requires
> importing a module and using the non-built-in features thereof,

Importing a module is not something to be afraid of. Python comes with
loads of modules especially so you can import them! :P

Also as of CPython 3.3 the decimal module is rewritten in C (which is
what is normally meant by "built-in").

> especially if you're going to do something like
> decimal.getcontext().prec (even that doesn't set precision AFTER the
> decimal point... only total precision). My point being that I don't
> see the value of sending a beginner into that morass.

Maybe it's tricky for a beginner. It is however good advice that you
should not use binary floating point for financial calculations.

> I actually still
> can't find how to force decimal to round to 3 places after the
> decimal...

I find myself using this:

>>> from decimal import Decimal as D
>>> D('0.1234567').quantize(D('1.000'))
Decimal('0.123')

Perhaps it would be better though to point at this:
>>> round(D('0.123456'), 3)
Decimal('0.123')

> (actually, I can't find a single reference to .prec except
> in examples in the standard library doc of decimal
> http://docs.python.org/3.3/library/decimal.html#decimal.getcontext)

.prec is "significant figures" rather than "decimal places" so it's
not what you want here.

> And finally: when I said "watch out for rounding", I meant be prepared
> for it. It is correct that rounding happens (otherwise the Snopes
> Salami scam happens), and I can't imagine that at the level at which
> the OP is working, any subtle control over rounding will be important
> (i.e. ROUNDING_CEILING or any such thing).

Some calculations need rounding in any fixed-width floating point
system. The decimal module let's you go up to 425 million digits
though:
>>> from decimal import MAX_PREC
>>> MAX_PREC
42500

More importantly it has traps that you can use to catch any example of
inexact computation:

>>> from decimal import localcontext, Inexact
>>> with localcontext() as ctx:
... ctx.traps[Inexact] = True
... D(1) / 3
...
Traceback (most recent call last):
  File "", line 3, in 
decimal.Inexact: []

This way you can feel confident that your results are exact or else
you'd have seen an error message.


Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Alan Gauld

On 25/01/14 21:19, Keith Winston wrote:

On Sat, Jan 25, 2014 at 3:57 AM, spir  wrote:

Note: AFAIK most financial software use integers for this reason and to
avoid (or control) rounding errors.


I don't think this is true


I can't speak for the general case but the only major financial app I've 
worked on (a mainframe billing system in COBOL) was done in pennies. It 
was a major rewrite of a legacy system  for the

"Millenium Bug". (It used the year 00 as a special case for
terminated accounts!! Amongst other foibles)...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Keith Winston
Also, just to be clear: I'd suggest floats because decimal requires
importing a module and using the non-built-in features thereof,
especially if you're going to do something like
decimal.getcontext().prec (even that doesn't set precision AFTER the
decimal point... only total precision). My point being that I don't
see the value of sending a beginner into that morass. I actually still
can't find how to force decimal to round to 3 places after the
decimal... (actually, I can't find a single reference to .prec except
in examples in the standard library doc of decimal
http://docs.python.org/3.3/library/decimal.html#decimal.getcontext)

And finally: when I said "watch out for rounding", I meant be prepared
for it. It is correct that rounding happens (otherwise the Snopes
Salami scam happens), and I can't imagine that at the level at which
the OP is working, any subtle control over rounding will be important
(i.e. ROUNDING_CEILING or any such thing).

Keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Oscar Benjamin
On 25 January 2014 21:01, Keith Winston  wrote:
>
> I think that you should probably do your math in floating point (why
> get complicated? And you might need the accuracy, for hundredths of
> dollars and interest) and then format the output to be what you want.
> Watch out for rounding.

It may not matter in this case but the key point about the Decimal
module is that it implements "decimal floating point" whereas ordinary
floats implement "binary floating point". I would prefer to use the
decimal module which can represent decimal values exactly and perform
simple decimal-type computations such as this one exactly.

Incidentally you can use the Decimal module to see how the *exact*
value of a float looks when written out in decimal digits:

>>> from decimal import Decimal as D
>>> D(0.5) # 0.5 can be represented exactly in binary FP
Decimal('0.5')
>>> D(0.1) # 0.1 cannot
Decimal('0.155511151231257827021181583404541015625')
>>> D(3.49)
Decimal('3.4902131628207280300557613372802734375')
>>> D(3.499)
Decimal('3.499110134124042815528810024261474609375')


Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Keith Winston
On Sat, Jan 25, 2014 at 3:57 AM, spir  wrote:
> Note: AFAIK most financial software use integers for this reason and to
> avoid (or control) rounding errors.

I don't think this is true (no flame intended, hopefully you know I'm
forever in your debt Denis): there's a famous scam where insiders at a
major financial institution set the software so it always rounds down,
and the difference was deposited in their account. It was a matter of
fractions of pennies on a per-scam basis. I'm not certain if this ever
actually happened (I thought it did, but Snopes seems agnostic).

http://www.snopes.com/business/bank/salami.asp

-- 
Keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Keith Winston
On Sat, Jan 25, 2014 at 3:57 AM, spir  wrote:
>> .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.4898

I think there's an inconsistency in your post that might confuse the
answers. You mention in the lead that you want to add 9 ONE HUNDREDTHS
of a dollar, or tenths of a cent (which is in fact how gas is priced
in the US, and yes it's crazy stupid). However in your example you add
only tenths, but then in the answer you appear to have added
hundredths, which makes me think that you didn't cut & paste, but
rather retyped (and mistyped).

This will make it a little trickier to use Denis' last idea of using
integers, since you'll have to take them out one more order of
magnitude. If this exercise is later followed by interest
calculations, or anything like that, you might regret limiting your
internal accuracy/representation.

I think that you should probably do your math in floating point (why
get complicated? And you might need the accuracy, for hundredths of
dollars and interest) and then format the output to be what you want.
Watch out for rounding.

>>> p = 3.499
>>> print('{0:.3f}'.format(p))   # format a float with 3 places after the 
>>> decimal
3.499
>>> p = 3.4994
>>> print('{0:.3f}'.format(p))
3.499
>>> p = 3.499
>>> print('{0:.3f}'.format(p))
3.500

-- 
Keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread Oscar Benjamin
On 25 January 2014 08:57, spir  wrote:
> On 01/25/2014 09:46 AM, spir wrote:
>>
>> On 01/24/2014 06:57 PM, Leon S 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.4898
>>>
>>> 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.
>>
>> This is instead easy for noone ;-)
>>
>> The core issue is that for, say, "fractional numbers" (numbers with a
>> fractional
>> part, but unlike real numbers with definite precision) python like most
>> programming languages uses in standard a binary representation internally.
>> While
>> we normally use decimal notation, both in input (reading) and output
>> (writing).
>> And there is no way to represent decimal fractions in binary, except in
>> the very
>> case where they are a multiple of an exact (negative) power of 2 (for
>> instance
>> 1/2, 3/4, 123/128... are ok).
>>
>> The only right solution is to use decimals internally, and python provides
>> such
>> a numeric type, Decimal:
>> http://docs.python.org/3/library/decimal.html
>
> I did not read correctly. If you're dealing with financial as it seems, the
> right way is to use integers instead.

I would say that using Decimal is the "right" solution for financial
calculations. Using integers (AKA fixed-point arithmetic) is possible
and is the standard solution for languages that lack a Decimal type.

> Since you are adding tenth's of pence,
> this is what your unit means. Then your sum is:
> 3490 + 9
> :-)
>
> Note: AFAIK most financial software use integers for this reason and to
> avoid (or control) rounding errors.

The Decimal module allows you to avoid or control rounding errors. If
it is true that most financial software uses fixed-point arithmetic
instead of floating point decimal arithmetic then I would guess that
the most likely reason is that the language used for the software
doesn't have a library for floating point decimal arithmetic as Python
does.

A significant motivating use-case for adding the Decimal module and
for subsequently speeding it up with a C implementation was to support
using Python in the context of financial software.

> At input and/or output, you may still have to convert to Decimal to get
> "decimally correct" value or expression.
>
> numeral = input("...")
> value = Decimal(numeral)
> ...
> output = Decimal(result) / Decimal("1000")
> print(output)
>
> (not sure, not tried)

I would do it like so:

>>> from decimal import Decimal
>>> def adjust_price(price):
... return Decimal(price) + Decimal('.005')
...
>>> adjust_price('3.49')
Decimal('3.495')
>>> print(adjust_price('3.49'))
3.495

But then what happens if the use has added the .005 themselves:

>>> print(adjust_price('3.495'))
3.500

Perhaps we should round down before adding .005:

>>> from decimal import ROUND_DOWN
>>> def adjust_price(price):
... # Round down to 2 decimal places
... price = Decimal(price).quantize(Decimal('1.00'), rounding=ROUND_DOWN)
... return price + Decimal('.005')
...
>>> print(adjust_price('3.49'))
3.495
>>> print(adjust_price('3.495'))
3.495

Of course now you'll get:

>>> print(adjust_price('3.497'))
3.495

Maybe that's not what was wanted either (I'm not sure exactly what you
do want in this case).


Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread spir

On 01/25/2014 09:46 AM, spir wrote:

On 01/24/2014 06:57 PM, Leon S 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.4898


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.


This is instead easy for noone ;-)

The core issue is that for, say, "fractional numbers" (numbers with a fractional
part, but unlike real numbers with definite precision) python like most
programming languages uses in standard a binary representation internally. While
we normally use decimal notation, both in input (reading) and output (writing).
And there is no way to represent decimal fractions in binary, except in the very
case where they are a multiple of an exact (negative) power of 2 (for instance
1/2, 3/4, 123/128... are ok).

The only right solution is to use decimals internally, and python provides such
a numeric type, Decimal:
http://docs.python.org/3/library/decimal.html


I did not read correctly. If you're dealing with financial as it seems, the 
right way is to use integers instead. Since you are adding tenth's of pence, 
this is what your unit means. Then your sum is:

3490 + 9
:-)

Note: AFAIK most financial software use integers for this reason and to avoid 
(or control) rounding errors.


At input and/or output, you may still have to convert to Decimal to get 
"decimally correct" value or expression.


numeral = input("...")
value = Decimal(numeral)
...
output = Decimal(result) / Decimal("1000")
print(output)

(not sure, not tried)

d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-25 Thread spir

On 01/24/2014 06:57 PM, Leon S 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.4898


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.


This is instead easy for noone ;-)

The core issue is that for, say, "fractional numbers" (numbers with a fractional 
part, but unlike real numbers with definite precision) python like most 
programming languages uses in standard a binary representation internally. While 
we normally use decimal notation, both in input (reading) and output (writing). 
And there is no way to represent decimal fractions in binary, except in the very 
case where they are a multiple of an exact (negative) power of 2 (for instance 
1/2, 3/4, 123/128... are ok).


The only right solution is to use decimals internally, and python provides such 
a numeric type, Decimal:

http://docs.python.org/3/library/decimal.html

d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-24 Thread Dave Angel
 Leon S  Wrote in message:
> _

(please post in plain text in this text mailing list.  Html messes
 up formatting and causes some newsreaders grief.)
..
Leon said:
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.4898

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
..

That stackoverflow link has lots of good information and some
 disinformation. 

First the code you show won't run, even after fixing the
 formatting.  Please use copy/paste. But making some assumptions, 
 I'm guessing you're adding two float objects and wondering why
 the result is off when printed. 

float is a binary floating point object and cannot represent
 precisely decimal float values like three point forty nine. You
 get quantization errors by definition,  and must deal with them
 somehow before the user sees them. You can fix that either by not
 using binary (perhaps the decimal module? ) or by rounding when
 you convert to string before printing. 

Next time, post in text format, include all your code (no print or
 equivalent in what you posted ), and tell us what version of
 python you're using. 



> 


-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-24 Thread wesley chun
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  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.4898
>
>
> 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  : wescpy at gmail :
@wescpy
Python training & consulting : http://CyberwebConsulting.com
"Core Python" books : http://CorePython.com
Python blog: http://wescpy.blogspot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to correct decimal addition.

2014-01-24 Thread Norman Khine
maybe this would be of help

https://stackoverflow.com/questions/455612/python-limiting-floats-to-two-decimal-points


On Fri, Jan 24, 2014 at 5:57 PM, Leon S  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.4898
>
>
> 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.
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>


-- 
%>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for
c in ",adym,*)&uzq^zqf" ] )
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] How to correct decimal addition.

2014-01-24 Thread Leon S
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.4898


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.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor