If I run this code:
for x in range(10):
time.sleep(1)
print("a")
It will do exactly what it should. But if I run this:
for x in range(10):
time.sleep(1)
print("a", end="")
It will wait the entire 10 seconds and then print the 10 a
's.
How can I prevent this?
Flush stdout after print.
import time
import sys
for x in range(10):
time.sleep(1)
print("a", end="")
sys.stdout.flush()
Python 3.3 print function has optional flush
parameter; You can write as follow in Python 3.3+.
import time
for x in range(10):
time.sleep(1)
print("a", end="", flush=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With