Python继续 [英] Python continue with while

查看:57
本文介绍了Python继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中继续操作似乎存在一些严重的问题: 例如:

There seems to be some serious problem with continue in Python: for eg:

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循环永远不会终止!

the while loop never terminates!

推荐答案

您的while循环与您的for循环的作用不同; for循环从 1 开始,并且始终递增i.

Your while loop does not do the same thing as your for loop; the for loop starts at 1 and always increments i.

在偶数测试之前将i += 1 移动:

i = 0

while(i < 10):
    i += 1
    if i % 2 == 0:   
        continue
    print i

因为0 % 2 == 0True,所以您总是继续,跳过i += 1print i语句.

because 0 % 2 == 0 is True, so you always continue, skipping the i += 1 and print i statements.

这篇关于Python继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆