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)]
現在應該工作
添加回答
舉報