【问题标题】:Is there a way to repeat a loop with same iteration?有没有办法用相同的迭代重复一个循环?
【发布时间】:2021-11-15 01:20:45
【问题描述】:

有没有办法用相同的迭代器重复一个循环,而 i 不增加 1?

    for i in range(100):
        random_function()
        if status_code != 200:
            #repeat function with same iterator without incrementing by 1. 

【问题讨论】:

标签: python loops if-statement repeat


【解决方案1】:

使用while循环并自己控制增量

i = 0
while i < 100:
    foo()
    if status == 200:
        i += 1

或者将重试逻辑构建到您的“随机函数”中

【讨论】:

  • 谢谢,我可以看到这背后的逻辑,但是如果迭代是针对一个列表而不是一个整数呢?
  • 然后使用while i &lt; len(list)
猜你喜欢
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-30
  • 2020-11-19
  • 2019-01-23
  • 1970-01-01
相关资源
最近更新 更多