int vs. float

Erik python at lucidity.plus.com
Fri Feb 10 15:46:16 EST 2017


On 10/02/17 19:15, Peter Pearson wrote:
 > On Fri, 10 Feb 2017 13:59:45 +0200, Amit Yaron <amit at phpandmore.net> 
wrote:
 >> Use:
 >>
 >> try:
 >>    num=int(str)
 >> except ValueError e:
 >>    print "Error: not an integer"
 >
 > What should happen if the user types "1.0"?
 >
 > To be flexible about this possibility, you could accept the number
 > as a float, and then complain if int(num) != num.

Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> 0.99999999999999995
1.0
 >>> f = float("0.99999999999999995")
 >>> i = int(f)
 >>> i
1
 >>> f
1.0
 >>> i == f
True
 >>>

E.



More information about the Python-list mailing list