Python while语句上的OSE子句我注意到以下代码在Python中是合法的。我的问题是为什么?有什么具体的原因吗?n = 5while n != 0:
print n
n -= 1else:
print "what the..."
3 回答

白板的微信
TA贡献1883条经验 获得超3个赞
else
while
break
if condition: handle_true()else: handle_false()
while condition: handle_true()else: # condition is false now, handle and go on with the rest of the program handle_false()
while value < threshold: if not process_acceptable_value(value): # something went wrong, exit the loop; don't pass go, don't collect 200 break value = update(value)else: # value >= threshold; pass go, collect 200 handle_threshold_reached()

人到中年有点甜
TA贡献1895条经验 获得超7个赞
else
break
return
for value in values: if value == 5: print "Found it!" breakelse: print "Nowhere to be found. :-("

Smart猫小萌
TA贡献1911条经验 获得超7个赞
Is there a specific reason?
for k in [2, 3, 5, 7, 11, 13, 17, 25]: for m in range(2, 10): if k == m: continue print 'trying %s %% %s' % (k, m) if k % m == 0: print 'found a divisor: %d %% %d; breaking out of loop' % (k, m) break else: continue print 'breaking another level of loop' breakelse: print 'no divisor could be found!'
while
for
else
break
添加回答
举报
0/150
提交
取消