Python print function issue

I am trying to execute 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 complete and then prints everything. If I use a regular font with \ n, in the end everything works as it should. But with the end = "" this is not so.

+4
source share
1 answer

stdout buffered by line. You must flush the output manually.

 import sys def sleep(sec): for i in range(sec): print(".", end=" ") sys.stdout.flush() time.sleep(1) 
+5
source

All Articles