int vs. float

boB Stepp robertvstepp at gmail.com
Sat Feb 11 02:47:32 EST 2017


On Sat, Feb 11, 2017 at 1:00 AM, Amit Yaron <amit at phpandmore.net> wrote:
> 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:

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

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

According to the OP's professor's challenge, the OP needs to recognize
an input of "4.0" as a float and "4" as an integer, and to respond
with an error message in the float case, or "decimal number" case as
the OP phrased it.  Apparently only positive integers are acceptable
input; all other inputs should generate an appropriate error message
by input type.


-- 
boB



More information about the Python-list mailing list