Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>case of 1x repetition: doStuff case of 2x repetition: doStuff, sleep, doStuff case of 3x repetition: doStuff, sleep, doStuff, sleep, doStuff ... </code></pre> <p>What you seem to want is to do a bunch of things, then sleep between each of them.</p> <p>Furthermore, the delay is independent of the nature of the tasks.</p> <hr> <p><strong>Solution 1 -- Sleep if it isn't the last</strong></p> <p>The easiest way is to special-case the for loop:</p> <pre><code>for i in range(numTimesToRepeat): if i&gt;0: time.sleep(lengthToSleep) doStuff() </code></pre> <p>You could also do the following, but it's much less elegant because you repeat <code>doStuff()</code>:</p> <pre><code>doStuff() for i in range(numTimesToRepeat-1): time.sleep(lengthToSleep) doStuff() </code></pre> <hr> <p><strong>Solution 2 -- While loop with break</strong></p> <p>One of the most straightforward ways to solve these issues is to break out of a while loop. It's dirty and somewhat non-functional-programming, but it's very much how a human might naturally think of the problem:</p> <pre><code>numTimesToRepeat = ... while True: doStuff() # we did stuff numTimesToRepeat -= 1 # repeat one less time because we just did it if numTimesToRepeat==0: # do we repeat again? break # no -&gt; done! else: time.sleep(lengthToSleep) # yes -&gt; sleep before repeating </code></pre> <p>While loops are annoying because if you make a programming error, your program will infinite-loop.</p> <hr> <p><strong>Solution 3 -- Detect if last</strong></p> <p>The above methods do something if it's not the first iteration of the loop. However, "detecting the last iteration of the loop" is how you tried to approach the problem. It is a bit more annoying, but you can do it like this: <a href="https://stackoverflow.com/a/6090673/711085">https://stackoverflow.com/a/6090673/711085</a> and use it like this:</p> <pre><code>for i,task,isLast in enumerateLast(tasks): task() if not isLast: time.sleep(lengthToSleep) </code></pre> <p>Less elegant is to reverse the list and do <code>enumerate()</code>:</p> <pre><code>for untilLast,task in reversed(enumerate(reversed(tasks))): task() if untilLast&gt;0: time.sleep(lengthToSleep) </code></pre> <p>(very small irrelevant minutiae: do note that in the <code>reversed(enumerate(reversed(iterable)))</code> case, if tasks were a generator, it would generate every single task to do, enumerate them in reverse order, then do them: this can cause lag, and also prevents you from creating an infinite task generator)</p> <hr> <p><strong>Task abstraction</strong></p> <p>No matter which way you do it, you may wish to abstract the concept of having tasks by creating callbacks, into which you insert a delay between each, sort of like a <code>''.join</code>.</p> <pre><code>def callAndWaitBetween(tasks): for ..task.. in enumerate?(tasks): #... some solution to this problem... task() </code></pre> <p>For reference, if the delay depended on the tasks, then your tasks should return how long to wait.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload