python 3 int' object has no attribute 'isdecimal'

Viewed 1455

Very new to python and trying to create a program which takes an inputed number and tells the user if the number is odd or even. I have part of it working, it takes an number and gives the correct output depending on weather its odd or even, but the problem is, if you enter anything but a number, the program dosn't recongise it and crashes the program.

My solution dosn't work and I don't understand the error message.

Enter any number above 0   45
Traceback (most recent call last):
line 5, in <module>
    if not number.isdecimal():
AttributeError: 'int' object has no attribute 'isdecimal'

My implementation:


while True:
    print()
    number = int(input("Enter any number above 0   ")) # Ask user to input a number

    if not number.isdecimal(): # Trying to get code to reject a input that is not a number instead of crashing the program
        print()
        print("Incorrect format, please enter numbers")
        print()
    else:
        if number % 2 == 0: # Tell user inputed number is even if number is even
            print()
            print("That number is even!")
        else:
            print() 
            print("That number is odd!")  # Tell user inputed number is odd if number is odd

4 Answers
if not number.isdecimal():
    print("incorrect")
else:
    number = int(number)
    if number % 2 == 0: # Tell user inputed number is even if number is even
        print()
        print("That number is even!")
    else:
        print() 
        print("That number is odd!")

The solution yo thos problem would require the use of the type method. So the solution would be as follows.

 If type(number) != int:

 Do something.

Hope this works.

The reason for this is .isdecimal() is a string function and you cannot apply it for an integer.

while True:
    print()
    number = (input("Enter any number above 0   ")) # Ask user to input a number

    if not number.isdecimal(): # Trying to get code to reject a input that is not a number instead of crashing the program
        print()
        print("Incorrect format, please enter numbers")
        print()
    else:
        if int(number) % 2 == 0: # Tell user input number is even if number is even
            print()
            print("That number is even!")
        else:
            print() 
            print("That number is odd!")  # Tell user input number is odd if number is odd

This should solve the problem.

I have not type cast the user input to int right away, instead it is now being done while checking if num%2==0.

The method isdecimal is a string method, seeing that is just verify if the string is a decimal and after that convert to a int.


string_input = input()

if not string_input.isdecimal():
    raise Exception("That is not a number!!!")
else:

    number = int(string_input)

    if number % 2 == 0: # Tell user inputed number is even if number is even
        print()
        print("That number is even!")
    else:
        print() 
        print("That number is odd!")  # Tell user inputed number is odd if number is odd

Related