Python 获取错误:';int';对象没有属性';isnumeric';

Python 获取错误:';int';对象没有属性';isnumeric';,python,if-statement,integer,do-while,isnumeric,Python,If Statement,Integer,Do While,Isnumeric,我写了一个代码来获得学生的平均分数,但我得到了一个错误。 下面是我的一段代码获取错误: scoreinput=input("Score lesson: ") while True: if scoreinput.isnumeric(): scoreinput=int(scoreinput) if scoreinput > 20: scoreinput = int(input("This number

我写了一个代码来获得学生的平均分数,但我得到了一个错误。 下面是我的一段代码获取错误:

scoreinput=input("Score lesson: ")
while True:
    if scoreinput.isnumeric():
        scoreinput=int(scoreinput)
        if scoreinput > 20:
            scoreinput = int(input("This number is too big. Try again: "))
        elif scoreinput < 0:
            scoreinput = int(input("This number is too low. Try again: "))
    else:
        print("please write number correctly...")
scoreinput=input(“分数课程:”)
尽管如此:
如果scoreinput.isnumeric():
scoreinput=int(scoreinput)
如果scoreinput>20:
scoreinput=int(输入(“此数字太大。请重试:”)
elif分数输入<0:
scoreinput=int(输入(“此数字太低。请重试:”)
其他:
打印(“请正确填写号码…”)
这是此代码的输出,该代码有一个错误:

Score lesson: 5
Traceback (most recent call last):
  File "e:\TEST PYTHON\test3.py", line 3, in <module>
    if scoreinput.isnumeric():
AttributeError: 'int' object has no attribute 'isnumeric
评分课程:5分
回溯(最近一次呼叫最后一次):
文件“e:\TEST PYTHON\test3.py”,第3行,在
如果scoreinput.isnumeric():
AttributeError:“int”对象没有属性“isnumeric”

请帮帮我。如果你在上面写:
scoreinput=int(输入('Score-lesson:')
你就不必验证
scoreinput
是数字还是字母。

如果输入是小于20的正数,这是你想要的,在
scoreinput=int(scoreinput)
您已经有了数字,但是没有做任何事情,而是继续while循环的下一次迭代。在下一次迭代中,
scoreinput
是一个
int
而不是
str
这就是为什么会出现错误。如果
scoreinput
在正确的范围内,则应使用
break
停止循环

当输入错误时,会出现另一个问题。如果输入不是一个数字,则不会获得新的输入,并将陷入无限循环。如果输入是一个数字,但不在0到20之间,则会得到新的输入并立即将其转换为
int
。如果输入不是数字,则会出现异常。如果它是一个数字,它将在您到达下一个迭代时失败,因为在迭代开始时,
scoreinput
应该是
str
,但它将是
int

我建议您使用以下代码:

while True:
    scoreinput=input("Score lesson: ")  # using input only one time at the beggining of the loop instead of input command for each case of bad input
    if scoreinput.isnumeric():
        scoreinput=int(scoreinput)
        if scoreinput > 20:
            print("This number is too big. Try again.")
        elif scoreinput < 0:
            print("This number is too low. Try again.")
        else:
            break  # input is valid
    else:
        print("please write number correctly...")
为True时:
scoreinput=input(“Score lesson:)#在循环开始时只使用一次输入,而不是针对每种输入错误的情况使用输入命令
如果scoreinput.isnumeric():
scoreinput=int(scoreinput)
如果scoreinput>20:
打印(“此数字太大,请重试。”)
elif分数输入<0:
打印(“此数字太低。请重试。”)
其他:
中断#输入有效
其他:
打印(“请正确填写号码…”)

我无法复制此内容。可能的原因是您正在2.x下运行代码,或者您在其他地方重新定义了
input