5

I am trying to this function:

def sleep(sec):
    for i in range(sec):
        print(".", end=" ");
        time.sleep(1);

the problem is that it waits for the for loop to finish then it prints everything. If I use the normal print with \n in the end everything works as it should. But with the end=" " it does not.

1 Answer 1

6

The stdout is line buffered. You need to flush the output manually.

import sys

def sleep(sec):
    for i in range(sec):
        print(".", end=" ")
        sys.stdout.flush()
        time.sleep(1)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.