int vs. float

Peter Pearson pkpearson at nowhere.invalid
Fri Feb 10 14:15:36 EST 2017


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.

-- 
To email me, substitute nowhere->runbox, invalid->com.



More information about the Python-list mailing list