Skip to content Skip to sidebar Skip to footer

Delay Between For Loop Iteration (python)

Is this possible in Python? I wrote a great loop/script in Python and I’d like to add this delay to it if at all possible. map(firefox.open, ['http://www.bing.com/search?q=' +

Solution 1:

You can do that using time.sleep(some_seconds).

from time import sleep

for i in range(10):
    print i
    sleep(0.5)    #in seconds

Implementation

Here is a cool little implementation of that: (Paste it in a .py file and run it)

from time import sleep

for i in range(101):
    print '\r'+str(i)+'% completed',
    time.sleep(0.1)

map(firefox.open, ['http://www.bing.com/search?q=' + str(i) for i in range(x))], [2] * x)


Solution 2:

Yes it is possible in python you should use 'time' module:

     >>> import time
     >>> list1=[1,2,3,4,5,6,7,8,9,10]
     >>> for i in list1:
         time.sleep(1)#sleep for 1 second 
         print i

output :

  1
  2
  3
  4
  5
  6
  7
  8
  9
  10

Solution 3:

Or if you want it to start on one and emulate a stopwatch:

import time
def count_to(number):
    for i in range(number):
        time.sleep(1)
        i += 1
        if i >= number:
            print('Time is up')
            break
    print(i)

Post a Comment for "Delay Between For Loop Iteration (python)"