Validation 为什么可以';我不能用这种方式验证用户输入吗?python

Validation 为什么可以';我不能用这种方式验证用户输入吗?python,validation,python-2.7,range,Validation,Python 2.7,Range,我试图验证一个用户输入,看看它是否在1到500之间。 以下是我提出的代码: while teamScore not in range (1,501): print "The score is not in the valid range!" teamScore=raw_input("Please enter the team score (1-500): ") 但是,当我运行代码时,除了声明0和900无效外,它不接受正确的数字,例如34、79、200 我希望仍然使用while循

我试图验证一个用户输入,看看它是否在1到500之间。 以下是我提出的代码:

while teamScore not in range (1,501):
    print "The score is not in the valid range!"
    teamScore=raw_input("Please enter the team score (1-500): ")
但是,当我运行代码时,除了声明0和900无效外,它不接受正确的数字,例如34、79、200

我希望仍然使用while循环,任何人都可以告诉我应该如何修改代码?
提前谢谢你

原始输入
返回包含用户输入的字符串。while循环使用该字符串并检查它是否在整数范围内。将
teamscore
变量转换为整数

while teamScore not in range (1,501):
    print "The score is not in the valid range!"
    teamScore=raw_input("Please enter the team score (1-500): ")
    try: 
         teamScore = int(teamScore)
    except ValueError:
         teamScore = 0

raw_input()返回一个字符串,您应该将其转换为int。

哦,是的,我现在看到了,难怪我上次可以这样做,这次不行!非常感谢你^^
while teamScore not in range (1,501):
    print "The score is not in the valid range!"
    teamScore=int(raw_input("Please enter the team score (1-500): "))