在python上检查.isdigit()
我一直收到这样一个错误:AttributeError,“int”对象没有属性“isdigit”。你能给我解释一下这意味着什么,我应该做什么或改变什么吗在python上检查.isdigit(),python,python-3.x,Python,Python 3.x,我一直收到这样一个错误:AttributeError,“int”对象没有属性“isdigit”。你能给我解释一下这意味着什么,我应该做什么或改变什么吗 def digi(): number = input("Choose a number: ") if number.isdigit() == True: number = int(number) if number > 100: print("number is g
def digi():
number = input("Choose a number: ")
if number.isdigit() == True:
number = int(number)
if number > 100:
print("number is greater than 100 =",number > 100)
elif number < 100:
print("number is greater than 100 =",number > 100)
else:
print("input must be 'int'")
digi()
def digi():
数字=输入(“选择一个数字:”)
如果number.isdigit()==True:
数字=整数(数字)
如果数量>100:
打印(“数字大于100=,数字大于100”)
elif编号<100:
打印(“数字大于100=,数字大于100”)
其他:
打印(“输入必须为“int”)
digi()
整数没有定义isdigit()isdigit()
是字符串内部的一个方法,用于检查字符串是否为有效整数
在python3中,input()
函数总是返回一个字符串,但是如果您遇到这个错误,则意味着该数字在某种程度上是一个int。您使用的是Python2
要解决您的困境,请将input()
替换为raw\u input()
或者只获取python3整数,而不定义isdigit()isdigit()
是字符串内部的一个方法,用于检查字符串是否为有效整数
在python3中,input()
函数总是返回一个字符串,但是如果您遇到这个错误,则意味着该数字在某种程度上是一个int。您使用的是Python2
要解决您的困境,请将input()
替换为raw\u input()
或者直接获取python3这可能就是您想要的,isdigit()
不是必需的
def digi():
number = input("Choose a number: ")
try:
number = int(number)
if number > 100:
print("number is greater than 100")
else:
print("number is less than or equal to 100")
except ValueError:
print("input must be 'int'")
digi()
这可能就是您要查找的内容,isdigit()
不是必需的
def digi():
number = input("Choose a number: ")
try:
number = int(number)
if number > 100:
print("number is greater than 100")
else:
print("number is less than or equal to 100")
except ValueError:
print("input must be 'int'")
digi()
根据您的要求,我建议进行以下修改。
def digi():
number=str(输入(“选择一个数字:”)
如果number.isdigit()==True:
数字=整数(数字)
如果数量>100:
打印(“数字大于100=,数字大于100”)
elif编号<100:
打印(“数字大于100=,数字大于100”)
其他:
打印(“输入必须为“int”)
输入将对您输入的内容进行评估,并返回评估结果
根据您的要求,我建议进行以下修改。
def digi():
number=str(输入(“选择一个数字:”)
如果number.isdigit()==True:
数字=整数(数字)
如果数量>100:
打印(“数字大于100=,数字大于100”)
elif编号<100:
打印(“数字大于100=,数字大于100”)
其他:
打印(“输入必须为“int”)
输入将对您输入的内容进行评估,并返回评估结果
按照此操作您实际上并不在Python 3上。获取Python 3。@user2357112谢谢。我只是这样做了,它成功了。使用isdecimal
而不是isdigit
(和Python 3)isdigit
是一种不安全的测试,因为它将unicode power-of-2,²等字符识别为无法转换为整数的数字。实际上,您并没有使用Python 3。获取Python 3。@user2357112谢谢。我只是这样做了,它成功了。使用isdecimal
而不是isdigit
(和Python 3)isdigit
是一种不安全的测试,因为它将unicode幂-of-2,²等字符识别为无法转换为整数的数字。@vaultah noted:)@vaultah noted:)