【问题标题】:How to start For-Loop from beginn if the If-Condition is True [duplicate]如何从 If-Condition 的开头开始 For-Loop 是 True [重复]
【发布时间】:2016-01-06 11:08:26
【问题描述】:

如果if 条件为真,我想知道如何从 0 开始 for-loop

for i in range (3):
   if a=1:
      #leave if-condition and start from beginning in for-loop with i=0

Break 在这里没有帮助,因为使用 break 我只能离开 if 条件,但我还想从头开始 for 循环。

【问题讨论】:

    标签: python python-3.x for-loop


    【解决方案1】:

    你可以试试这个而不是 for 循环。

    i = 0
    while i < 3:
        if a == 1:
            i = 0
        i += 1
    

    如果 a 等于 1,这将继续遍历循环

    【讨论】:

      【解决方案2】:

      我觉得你可以这样写

      if 1 == a:
          for i in range(3):
              pass
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-15
        • 2013-12-08
        • 2014-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多