[英]Don't understand why loop isn't stopping
I'm using a while loop in Python to do an action.我在 Python 中使用 while 循环来执行操作。 However, the loop doesn't end when I expect it to (when teams becomes empty).
但是,循环并没有在我期望的时候结束(当团队变空时)。 This is the code:
这是代码:
while teams:
if scores[0]<scores[1]:
losers.append(teams[0])
teams.remove(teams[0])
teams.remove(teams[0])
scores.remove(scores[0])
scores.remove(scores[0])
if scores[0]>scores[1]:
losers.append(teams[1])
teams.remove(teams[0])
teams.remove(teams[0])
scores.remove(scores[0])
scores.remove(scores[0])
return losers
While testing the code, I put in print statements and found that the losers array has the expected result, however the loop continues after I expect it to stop, checking for scores[0] when I already deleted scores[0] on the last iteration of the loop.在测试代码时,我放入了打印语句,发现输家数组具有预期的结果,但是在我希望它停止后循环继续,当我在最后一次迭代中删除了分数 [0] 时检查分数 [0]的循环。 Why is my loop not ending?
为什么我的循环没有结束?
Double check the conditions of the loop.仔细检查循环的条件。 Loops will become infinite if a condition never becomes equal to
false
.如果条件永远不会等于
false
则循环将变得无限。
See the "The Infinite Loop" section of http://www.tutorialspoint.com/python/python_while_loop.htm .请参阅http://www.tutorialspoint.com/python/python_while_loop.htm的“无限循环”部分。
If the loop does not end is because teams
never becomes empty.如果循环没有结束是因为
teams
永远不会变空。 Your assumption "the loop doesn't end when [...] teams
becomes empty" is incorrect and probably misguided you in your attempts to solve the problem.您的假设“当 [...]
teams
变空时循环不会结束”是不正确的,并且可能在您尝试解决问题时误导了您。
The fix is to change the second if
statement to elif
:解决方法是将第二个
if
语句更改为elif
:
while teams:
if scores[0]<scores[1]:
losers.append(teams[0])
teams.remove(teams[0])
teams.remove(teams[0])
scores.remove(scores[0])
scores.remove(scores[0])
elif scores[0]>scores[1]:
losers.append(teams[1])
teams.remove(teams[0])
teams.remove(teams[0])
scores.remove(scores[0])
scores.remove(scores[0])
return losers
The problem is that when the teams
and scores
lists get to the last pair, and the condition in the first if
statement is True
, that final pair is removed and both lists become empty.问题是当
teams
和scores
列表到达最后一对并且第一个if
语句中的条件为True
时,最后一对被删除并且两个列表都变为空。
Then the second if
condition is evaluated, but the scores
list is now empty (courtesy of the preceding if
matching), so an IndexError
is raised.然后评估第二个
if
条件,但scores
列表现在是空的(由前面的if
匹配提供),因此引发IndexError
。
Changing it to an elif
means that the second condition will only be evaluated if the first condition is False
.将其更改为
elif
意味着仅当第一个条件为False
才会评估第二个条件。 Now the loop will only process one pair of teams/scores per iteration, and the test for an empty team
will succeed.现在循环每次迭代只会处理一对团队/分数,并且对空
team
的测试将成功。
You could make the while loop a bit clearer with this:您可以通过以下方式使 while 循环更清晰:
while teams:
if scores[0] < scores[1]:
losers.append(teams[0])
elif scores[0] > scores[1]:
losers.append(teams[1])
else:
# this should never happen
print('Error: drawn match detected!')
losers.append(None) # or simply ignore if that makes sense
teams[:] = teams[2:] # remove the first 2 items
scores[:] = scores[2:]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.