Sleeping and printing without newline?

python

Solution

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)

Problem

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?

Original source

Related problems