0

I keep getting the error saying AttributeError, 'int' object has no Attribute 'isdigit'. Could you explain to me what that means, and what I should do or change?

def digi():

    number = input("Choose a number: ")

    if number.isdigit() == True:
        number = int(number)
        if number > 100:
            print("number is greater than 100 =",number > 100)

        elif number < 100:
            print("number is greater than 100 =",number > 100)


    else:
        print("input must be 'int'")


digi()
vaultah
  • 44,105
  • 12
  • 114
  • 143
  • 3
    You're not actually on Python 3. Get Python 3. – user2357112 Apr 09 '18 at 23:06
  • @user2357112 Thanks. I just did that and it worked. –  Apr 09 '18 at 23:22
  • Use `isdecimal` not `isdigit` (and Python 3). `isdigit` is an unsafe test because it recognises characters like unicode power-of-2, ² , as a digit which can not be converted to integers. – Dave Rove Jul 16 '20 at 11:42

3 Answers3

3

ints do not have isdigit() defined. isdigit() is a method inside of strings that checks if the string is a valid integer.

In python3 the input() function always returns a string, but if you are getting that error that means that number was somehow an int. You are using python 2.

To fix your predicament replace input() with raw_input().

Or just get python3

Primusa
  • 13,136
  • 3
  • 33
  • 53
0

This might be what you're looking for, isdigit() is NOT required.

def digi():
    number = input("Choose a number: ")

    try:
        number = int(number)
        if number > 100:
            print("number is greater than 100")

        else:
            print("number is less than or equal to 100")

    except ValueError:
        print("input must be 'int'")

digi()
Michael Swartz
  • 858
  • 2
  • 15
  • 27
0

As per your requirement, I would suggest the following modification. def digi():

number = str(input("Choose a number: "))

if number.isdigit() == True:
    number = int(number)
    if number > 100:
        print("number is greater than 100 =",number > 100)

    elif number < 100:
        print("number is greater than 100 =",number > 100)


else:
    print("input must be 'int'")

Input will evaluate whatever you entered and the result of evaluation will be returned.

Follow this link

Shivam Chawla
  • 390
  • 5
  • 17