Python AttributeError: 'int' 对象没有属性 'isdigit'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33049167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:40:05  来源:igfitidea点击:

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

python

提问by Kristofer Ard

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'

AttributeError: 'int' 对象没有属性 '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.

由于我是编程新手,我真的不知道它想告诉我什么。我正在使用if cpi.isdigit():来检查用户输入的数字是否有效。

采纳答案by Ben

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

回答by marmeladze

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

正如这里记录的那样isdigit()是一个字符串方法。您不能为整数调用此方法。

This line,

这条线,

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

evaluatesthe user input to integer.

evaluates用户输入为整数。

>>> 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 evalmethod (you should better do that),

但是如果你删除eval方法(你最好这样做),

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

顺便说一下,在没有 sanitizin 用户输入的情况下使用 eval 可能会导致问题

consider this.

考虑这个。

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

回答by Kevin Guan

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

eval()很危险!并且int()内置函数可以将字符串转换为数字。

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

如果你想在用户没有输入数字的情况下捕获错误,只需try...except像这样使用:

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.")

回答by usr_11

Use this:

用这个:

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

isdigit()works only for strings.

isdigit()仅适用于字符串。