简体   繁体   English

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

[英]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' 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():检查用户输入的内容是否为有效数字。

As documented here isdigit() is a string method. 正如此处所述, isdigit()是一个字符串方法。 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. 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 eval method (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' ...

Use this: 用这个:

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

isdigit() works only for strings. isdigit()仅适用于字符串。

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

eval() is very dangerous! eval()非常危险! And int() built-in function can convert string to digit. int()内置函数可以将字符串转换为数字。

If you want to catch the error if user didn't enter a number, just use try...except like 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.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: &#39;int&#39; 对象没有属性 &#39;isdigit&#39; 和 NameError - AttributeError: 'int' object has no attribute 'isdigit' and NameError 为什么我的代码不起作用 AttributeError: 'int' object has no attribute 'isdigit' - why my code not working AttributeError: 'int' object has no attribute 'isdigit' AttributeError:&#39;int&#39;对象没有用户输入的属性&#39;isdigit&#39; - AttributeError: 'int' object has no attribute 'isdigit' from user input AttributeError&#39;int&#39;对象在Ubuntu中的ipython中没有属性isdigit - AttributeError 'int' object has no attribute isdigit in ipython in ubuntu AttributeError: 'generator' object 没有属性 'isdigit' - AttributeError: 'generator' object has no attribute 'isdigit' Python-AttributeError:“ str”对象没有属性“ isDigit” - Python - AttributeError: 'str' object has no attribute 'isDigit' Python AttributeError:“系列”对象没有属性“ isdigit” - Python AttributeError: 'Series' object has no attribute 'isdigit' AttributeError:“列表”对象没有属性“ isdigit” - AttributeError: 'list' object has no attribute 'isdigit' 持续收到错误-builtins.AttributeError:“ int”对象没有属性“ isdigit” - Keep receiving error- builtins.AttributeError: 'int' object has no attribute 'isdigit' AttributeError:int对象没有属性 - AttributeError : int object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM