2 回答
TA贡献1850条经验 获得超11个赞
更改此部分:
for x in record:
while( x < -100 or x > 100):
scores = input("Error. Scores should be -100 to 100 only. Please enter scores again separated by space: ")
data = list(map(int, scores.split()))
record = data[slice(players)]
到:
while any( x < -100 or x > 100 for x in record):
scores = input("Error. Scores should be -100 to 100 only. Please enter scores again separated by space: ")
data = list(map(int, scores.split()))
record = data[slice(players)]
您的代码不起作用的原因是:
for x in record:
while( x < -100 or x > 100):
您正在循环使用那个特定的x. 更新时record,具体x内容将保持不变,因此while循环永远不会中断。
TA贡献1893条经验 获得超10个赞
这是根据您的目的编写代码的正确方法:
players = int(input("Enter number of players: "))
while (players < 2 or players > 10):
players = int(input("Error. Players should be 2-10 only. Enter number of players: "))
continue
现在它会不停地问你,直到玩家人数为 2 - 10。
并更改以下代码:
while any(x < -100 or x > 100 for x in record):
scores = input("Error. Scores should be -100 to 100 only. Please enter scores again separated by space: ")
data = list(map(int, scores.split()))
record = data[slice(players)]
现在应该工作
添加回答
举报