Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8431117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:46:13+00:00 2026-06-10T05:46:13+00:00

i have for i in xrange(repeat): #does my stuff time.sleep(10) BUT, i don’t want

  • 0

i have

for i in xrange(repeat):
  #does my stuff
  time.sleep(10)

BUT, i don’t want it to sleep if it’s the last run. Meaning, if there’s no more iteration, don’t sleep

How do I gracefully handle this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:46:15+00:00Added an answer on June 10, 2026 at 5:46 am
    case of 1x repetition: doStuff
    case of 2x repetition: doStuff, sleep, doStuff
    case of 3x repetition: doStuff, sleep, doStuff, sleep, doStuff
    ...
    

    What you seem to want is to do a bunch of things, then sleep between each of them.

    Furthermore, the delay is independent of the nature of the tasks.


    Solution 1 — Sleep if it isn’t the last

    The easiest way is to special-case the for loop:

    for i in range(numTimesToRepeat):
        if i>0:
            time.sleep(lengthToSleep)
        doStuff()
    

    You could also do the following, but it’s much less elegant because you repeat doStuff():

    doStuff()
    for i in range(numTimesToRepeat-1):
        time.sleep(lengthToSleep)
        doStuff()
    

    Solution 2 — While loop with break

    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:

    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 -> done!
        else:
            time.sleep(lengthToSleep) #   yes -> sleep before repeating
    

    While loops are annoying because if you make a programming error, your program will infinite-loop.


    Solution 3 — Detect if last

    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: https://stackoverflow.com/a/6090673/711085 and use it like this:

    for i,task,isLast in enumerateLast(tasks):
        task()
        if not isLast:
            time.sleep(lengthToSleep)
    

    Less elegant is to reverse the list and do enumerate():

    for untilLast,task in reversed(enumerate(reversed(tasks))):
        task()
        if untilLast>0:
            time.sleep(lengthToSleep)
    

    (very small irrelevant minutiae: do note that in the reversed(enumerate(reversed(iterable))) 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)


    Task abstraction

    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 ''.join.

    def callAndWaitBetween(tasks):
        for ..task.. in enumerate?(tasks):
            #... some solution to this problem...
            task()
    

    For reference, if the delay depended on the tasks, then your tasks should return how long to wait.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code, but I don't know how can I make it looks
Apparently xrange is faster but I have no idea why it's faster (and no
I have an double array alist[1][1]=-1 alist2=[] for x in xrange(10): alist2.append(alist[x]) alist2[1][1]=15 print
have written this little class, which generates a UUID every time an object of
I have this problem since a long time and I cannot find anything to
I have a generator that I want to iterate through at two levels. The
I have a script that creates a random string and I want to turn
I have a Graphing Calculator plugin in my app written in CorePlot. And there's
I have a collection of n dimensional points and I want to find which
Ok, the code I have is the following: for shidx in xrange(0, book.nsheets): print

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.