如果由于某种原因我想重复相同的迭代,我该如何在python中做呢?
for eachId in listOfIds:
#assume here that eachId conatins 10
response = makeRequest(eachId) #assume that makeRequest function request to a url by using this id
if response == 'market is closed':
time.sleep(24*60*60) #sleep for one day
现在当功能在一天后(市场(货币交易市场)开放)从 sleep 中唤醒时,我想从
eachId = 10
的not
eachId = 11
恢复我的for循环,因为eachId = 10
尚未被处理为market was closed
,因此非常感谢您的帮助。 最佳答案
像这样做:
for eachId in listOfIds:
successful = False
while not successful:
response = makeRequest(eachId)
if response == 'market is closed':
time.sleep(24*60*60) #sleep for one day
else:
successful = True
您问题的标题就是线索。重复是通过迭代实现的,在这种情况下,您可以使用嵌套的
while
简单地进行重复。关于python - 重复for循环的迭代,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7293978/