user9126689
user9126689

Reputation:

Checking .isdigit() on python

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

Upvotes: 1

Views: 5730

Answers (3)

Shivam Chawla
Shivam Chawla

Reputation: 458

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

Upvotes: 0

Michael Swartz
Michael Swartz

Reputation: 858

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

Upvotes: 0

Primusa
Primusa

Reputation: 13498

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

Upvotes: 4

Related Questions