在某一点评估while循环?

时间:2016-10-24 02:26:23

标签: python python-3.x while-loop conditional-statements

我是否有某种方法可以检查Python中的循环内部是否仍然存在while循环条件?像这样:

i = 0
while i < 2:
    i = 2
    evalcond
    print("This executes because the condition is still true at this point")

这可能吗?

2 个答案:

答案 0 :(得分:1)

或许这样的事情?

i = 0
while i < 2:
    i = 2
    if i >= 2:
        break
    print("This executes because the condition is still true at this point")

答案 1 :(得分:0)

如果您想避免使用break,也可以执行此操作

i = 0
while i < 2:
    i = 2
    if i < 2:
        print("This executes because the condition is still true at this point")