python `print` does not work in loop
The problem using print <something>,
is buffering and printing only when the result id ready to be printed.
You can solve it using print_function
from __future__
(which will be in compliance with Python 3 as well):
from __future__ import print_function
from time import sleep
import sys
for i in range(10):
print(i, end='')
for j in range(-5,5):
if j > 0:
print('.', end='')
else:
print('D', end='')
sys.stdout.flush()
sleep(1)
print('')
Because of existence of comma, the output buffers until a \n
.
You should flush the stdout
after every print or use sys.stdout.write
and flush buffer.
Define your print method:
import sys
def my_print(text):
sys.stdout.write(str(text))
sys.stdout.flush()
and at the end of line print a \n