close

[Solved] Attribute: ‘int’ object has no attribute ‘isdigit’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Attribute: ‘int’ object has no attribute ‘isdigit’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Attribute: ‘int’ object has no attribute ‘isdigit’ Error Occurs?

Today I get the following error Attribute: ‘int’ object has no attribute ‘isdigit’ in python.

How To Solve Attribute: ‘int’ object has no attribute ‘isdigit’ Error ?

  1. How To Solve Attribute: 'int' object has no attribute 'isdigit' Error ?

    To Solve Attribute: 'int' object has no attribute 'isdigit' Error As documented here isdigit() is a string method. You can't call this method for integers.

  2. Attribute: 'int' object has no attribute 'isdigit'

    To Solve Attribute: 'int' object has no attribute 'isdigit' Error As documented here isdigit() is a string method. You can't call this method for integers.

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

Solution 2

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' ...

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read