python `print` does not work in loop -
i have multi loops in , sleep in inner loop. example:
from time import sleep in range(10): print i, j in range(-5,5): if j > 0: print '.', else: print 'd', sleep(1) print '' if run code, may expected i value after d sleep 1 second , d , again sleep until end.
but result difference, waits 10 seconds , prints whole line of 0 d d d d d d . . . . , waiting again printing next line.
i found comma @ end of printing causes problem. how can solve it?
because of existence of comma, output buffers until \n.
you should flush stdout after every print or use sys.stdout.write , flush buffer.
define print method:
import sys def my_print(text): sys.stdout.write(str(text)) sys.stdout.flush() and @ end of line print \n
Comments
Post a Comment