Attributeerror: 'Int' Object Has No Attribute 'Isdigit'
numOfYears = 0
cpi = eval(input("Enter the CPI for July 2015: "))
if cpi.isdigit():
    while cpi < (cpi * 2):
        cpi *= 1.025
        numOfYears += 1
    print("Consumer prices will double in " + str(numOfYears) + " years.")
while not cpi.isdigit():
    print("Bad input")
    cpi = input("Enter the CPI for July 2015: ")

I'm getting the following error.

AttributeError: 'int' object has no attribute 'isdigit'

Since I'm new to programming, I don't really know what it's trying to tell me. I'm using the if cpi.isdigit(): to check to see if what the user entered is a valid number.

6

4 Answers

As documented here isdigit() is a string method. You can't call this method for integers.

This line,

cpi = eval(input("Enter the CPI for July 2015: ")) 

evaluates the user input to integer.

>>> x = eval(input("something: "))
something: 34
>>> type(x)
<class 'int'>
>>> x.isdigit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'isdigit'

But if you remove eval method (you should better do that),

>>> x = input("something: ")
something: 54
>>> type(x)
<class 'str'>
>>> x.isdigit()
True

everything will be fine.

by the way using eval without sanitizin user input may cause problems

consider this.

>>> x = eval(input("something: "))
something: __import__('os').listdir()
>>> x
['az.php', 'so', 'form.php', '.htaccess', 'action.php' ...
2

Use this:

if(str(yourvariable).isdigit()) :
    print "number"

isdigit() works only for strings.

1
numOfYears = 0
# since it's just suppposed to be a number, don't use eval!
# It's a security risk
# Simply cast it to a string
cpi = str(input("Enter the CPI for July 2015: "))

# keep going until you know it's a digit
while not cpi.isdigit():
    print("Bad input")
    cpi = input("Enter the CPI for July 2015: ")

# now that you know it's a digit, make it a float
cpi = float(cpi)
while cpi < (cpi * 2):
    cpi *= 1.025
    numOfYears += 1
# it's also easier to format the string
print("Consumer prices will double in {} years.".format(numOfYears))
1

eval() is very dangerous! And int() built-in function can convert string to digit.

If you want to catch the error if user didn't enter a number, just use try...except like this:

numOfYears = 0

while numOfYears == 0:
    try:
        cpi = int(input("Enter the CPI for July 2015: "))
    except ValueError:
        print("Bad input")
    else:
        while cpi < (cpi * 2):
            cpi *= 1.025
            numOfYears += 1

print("Consumer prices will double in", numOfYears, "years.")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest