简体   繁体   中英

AttributeError 'int' object has no attribute isdigit in ipython in ubuntu

def input_base():
    print('please enter the number')
    base = input("Number : ")
    while not base.isdigit():
       print("It`s not integer")
       base = input("R.Number : ")
    return base 
    ...

This is my code and the error is:

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

I don't know how I could fix this code. I think, I should install some application, such a python-numpy , inside Ubuntu...
Is that right?

You are using python2 therefore you have two functions input and raw_input . The difference being that input calls eval on the inputted string. This turns it from a string to whatever python would interpret it as when input as a script or at the REPL.

For the input 1 you would get the int value 1.

So the value you now have is an int and not a string. It does not have a method isdigit . If you are sticking with python 2, you should instead use raw_input , which doesnt do the eval and therefore always returns a string, which has an isdigit method.

For python3 input does what raw_input does it py2 and so works correctly here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM