int vs. float
Amit Yaron
amit at phpandmore.net
Sat Feb 11 02:00:22 EST 2017
On 10/02/17 21:15, Peter Pearson wrote:
> On Fri, 10 Feb 2017 13:59:45 +0200, Amit Yaron <amit at phpandmore.net> wrote:
>> On 10/02/17 04:33, adam14711993 at gmail.com wrote:
>>> Hello,
>>>
>>> My computer programming professor challenged me to figure out a way
>>> to manipulate my program to display one error message if the user
>>> input is a zero or a negative number, and a separate error message if
>>> the user input is a decimal number. My program starts out:
> [snip]
>> The function int(str) raises the exception ValueError if the input
>> string does not represent an integer.
>>
>> 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.
>
Another option:
Use 'float' instead of 'int'. and check using the method 'is_integer'
of floating point numbers:
>>> 3.5.is_integer()
False
>>> 4.0.is_integer()
True
More information about the Python-list
mailing list