Repeat for loop

if for some reason I want to repeat the same iteration, how can I do this in 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 

now that the function wakes up after sleep one day later (the market (currency trading market) is open), I want to resume the for loop from eachId = 10 not from eachId = 11 , because eachId = 10 did not yet exist was processed as market was closed , any help was greatly appreciated thanks.

+8
python for-loop
source share
4 answers

Do it like this:

 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 

The name of your question is the key. Repetition is done through iteration, in which case you can do it simply with a nested while .

+18
source share

Use while ?

 counter = 0 while counter < len(listOfIds): # do processing counter = counter + 1 

And just do not increase if you get a "market closed."

+3
source share
 for eachId in listOfIds: while makeRequest(eachId) == 'market is closed': time.sleep(24*60*60) #sleep for one day 

As @David is added, if you don't need to write response .

0
source share
 i = 0 while i < len(listOfIds): eachId = listOfIds[i] #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 else: i += 1 
0
source share

All Articles