Python 中的 continue 似乎存在一些严重问题:例如:
for i in range(1,10):
if i % 2 == 0:
continue
print i
按预期工作,但是
i = 0
while(i < 10):
if i %2 == 0:
continue
i += 1
print i
while 循环永远不会终止!
您i
在第二个片段中永远不会增加。删除继续。
i = 0
while(i < 10):
if i %2 == 0: # i == 0; continue without increment the value of i <-- stuck here!
continue
i += 1
print i
您的while
循环与您的for
循环不同;for
循环从1开始并始终递增i
。
在偶数测试i += 1
之前移动:
i = 0
while(i < 10):
i += 1
if i % 2 == 0:
continue
print i
因为0 % 2 == 0
is True
,所以你总是继续,跳过i += 1
andprint i
语句。