【问题标题】:Evaluate while loop at certain point?在特定点评估while循环?
【发布时间】:2017-03-05 17:27:17
【问题描述】:

是否有某种方法可以检查 Python 循环内的 while 循环条件是否仍然为真?像这样的:

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

这可能吗?

【问题讨论】:

  • 看起来你想要一个 while 1:if:... break 结构。

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


【解决方案1】:

大概是这样的吧?

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

【讨论】:

    【解决方案2】:

    如果你想避免使用break,你也可以这样做

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多