int vs. float

Amit Yaron amit at phpandmore.net
Sat Feb 11 06:07:21 EST 2017


On 11/02/17 09:47, boB Stepp wrote:
> 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.
>
>
Here's the output of the int function with a  string argument:
 >>> int("4.0")
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>

You can use exception handling to detect numbers that do not match the 
regular expression for integers, and comparisons to check if a number is 
negative.






More information about the Python-list mailing list