Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

repeat an iteration of for loop

Tags:

if for some reason i want to repeat the same iteration how i can do it 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 when the function wake up from sleep after one day (market (currency trade market) is open) i want to resume my for loop from eachId = 10 not from eachId = 11, because eachId = 10 is not yet been processed as market was closed, any help is highly appreciated thanks.

like image 800
Aamir Rind Avatar asked Sep 03 '11 15:09

Aamir Rind


People also ask

Can you repeat a for loop in Python?

Loops let you easily repeat tasks or execute code over every element in a list. A for loop enables you to repeat code a certain amount of time. A while loop lets you repeat code until a certain condition is met.

Does a for loop repeat?

A "For" Loop is used to repeat a specific block of code a known number of times.

How do you repeat an iteration in a loop C++?

{ work(i); if (repeat) { work(i); } } . Or even for (int i...) { again: work(i); if (repeat) goto again; } !


1 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 title of your question is the clue. Repeating is achieved by iteration, and in this case you can do it simply with a nested while.

like image 174
David Heffernan Avatar answered Sep 21 '22 05:09

David Heffernan