[英]Python - AttributeError: 'str' object has no attribute 'isDigit'
Here is my code 这是我的代码
input = raw_input("Please enter a number")
print input.isDigit()
when I feed in the input, 7 into the interpreter it throws the error: 当我在解释器中输入7时,它会引发错误:
AttributeError: 'str' object has no attribute 'isDigit'
This makes no sense since isDigit
is a built in function and is for strings. 这是没有道理的,因为
isDigit
是内置函数,适用于字符串。 I am using python 2.7 and I am using JetBrains Pycharm so could it be the IDE I'm using? 我正在使用python 2.7并且正在使用JetBrains Pycharm,所以它可能是我正在使用的IDE吗?
D
must be in small letter( lowercase letter ). D
必须为小写字母 ( 小写字母 )。
print input.isdigit()
And don't use in-built keywords as variable names. 并且不要将内置关键字用作变量名。
>>> inp = raw_input("Please enter a number: ")
Please enter a number: 5
>>> print inp.isdigit()
True
var= '10'
if(var.isdigit()):
print "has only digits"
else:
print "has something else"
you are using isdigit() method incorrectly, remember python is case sensitive and you must use lowercase d in isdigit(). 您错误地使用了isdigit()方法,请记住python区分大小写,并且必须在isdigit()中使用小写字母d。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.