Repeating A Function A Set Amount Of Times In Python
Solution 1:
First let me double check if I understood what you need: you have to place numTries sequential calls to tryConfiguration(floorplan,numLights), and each call is the same as the others.
If it is so, and if tryConfiguration is synchronous, you can just use a for loop:
for _ in xrange(numTries):
tryConfiguration(floorplan,numLights)
Please let me know if I'm missing something: there could be other solutions, like leveraging closures and/or recursion, if your requirements are different.
Solution 2:
Loop in the range of numTries and call the function each time.
for i in range(numTries):
tryConfiguration(floorplan,numLights)
If using python2 use xrange to avoid creating the whole list in memory.
Basically you are doing:
In [1]: numTries = 5
In [2]: for i in range(numTries):
...: print("Calling function")
...:
Calling function
Calling function
Calling function
Calling function
Calling functionSolution 3:
When we're talking about repeating a certain block of code multiple times, it's generally a good idea to use a loop of some kind.
In this case you could use a "for-loop":
for unused in range(numtries):
tryConfiguration(floorplan, numLights)
A more intuitive way (albeit clunkier) might be using the while loop:
counter = 0
while counter < numtries:
tryConfiguration(floorplan, numLights)
counter += 1
Post a Comment for "Repeating A Function A Set Amount Of Times In Python"