即使我已经输入了从-100到100的分数,我仍然卡住了。为什么会这样呢?请帮我把它修好!
players = int(input("Enter number of players: "))
while (players < 2 or players > 10): #Limits number of players to 2-10 only
players = int(input("Error. Players should be 2-10 only. Enter number of players: "))
scores = input("Enter scores separated by space: ")
data = list(map(int, scores.split()))
record = data[slice(players)]
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)]
record.sort(reverse= True)
values = []
for x in record:
if x not in values:
values.append( x )
if len(values) == 3:
break
print ("The runner-up score is:",values[1]) 这就是发生的事情:
Enter number of players: 3
Enter scores separated by space: 10000 2 3
Error. Scores should be -100 to 100 only. Please enter scores again separated by space: 233 4 5
Error. Scores should be -100 to 100 only. Please enter scores again separated by space: 1 2 3
Error. Scores should be -100 to 100 only. Please enter scores again separated by space: 如你所见,第三次我已经输入了1 2 3,但它仍然显示错误。
请帮帮我:(非常感谢你的帮助!
发布于 2020-06-28 02:11:49
更改此部分:
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循环永远不会中断。
发布于 2020-06-28 01:22:51
这是编写代码的正确方式:
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)] 现在应该可以工作了
https://stackoverflow.com/questions/62613058
复制相似问题