0

I'm trying to use the sleep function to have a delay between iterations in a loop but the loop executes all at once without delays.

from time import sleep


for i in range(5):
    print (i)
    sleep(0.5)

I tried doing it without a loop and the same problem, prints all the text instantly:

from time import sleep

print('hi')
sleep(2)
print('hi')
sleep(2)
print('hi')
sleep(2)

Edit: The issue is indeed output buffering. Adding sys.stdout.flush() after every print line fixed the issue.

Alan Navai
  • 47
  • 11

2 Answers2

2
        for i in range(5):
            print(i)
            sleep(5)

do it like this, it is working in my side

2

I believe this is because of output buffering. You can find the solution here: Disable output buffering

Jarett LeVan
  • 364
  • 1
  • 10