Is_int


#1

Hi All,

I was just going through some of the solutions posted in Q&A forum for is_int
exercise and works fine when we hard code the input.
But looks like it crashes when we ask the user to input the values and check
for all the conditions.

Any better solutions to handle this situation as well?.

Thanks


#2

you could use try/except, it allows you to catch errors:

https://docs.python.org/2/tutorial/errors.html

don't forget that when prompting with raw_input the result is stored a string

not sure which of this two is the problem, given you didn't include any code


#3

Thanks for the reply.
Figured it out the solution to handle my previous query.

Below is my code with output:

from decimal import Decimal
def is_int(x):
    x = Decimal(x)
    if abs(x) % 1 > 0:
        return False
    else:
        return True
    
x = input("Enter the input:")
print (is_int(x))

Output

Enter the input:5
True
Enter the input:5.1
False
Enter the input:5.0
True
Enter the input:-5
True
Enter the input:-5.1
False


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.