Simple (?) question about print statement

Grant Edwards grante at visi.com
Wed Dec 14 21:56:12 CET 2005


On 2005-12-14, TY <aqteon at yahoo.com> wrote:

> for i in range(10):
>     for j in range(5000000): pass     # Timing-delay loop
>     print i,
>
> Now it does this:
>
><long pause> then prints 0 1 2 3 4 5 6 7 8 9 all at once.
>
> Why?????
>
> How can I make it to print each numbers on the same line with pauses in
> between them?

I think there's some way to make "print" unbuffered, but I
don't remember how to do that.

I do do know this will work:

for i in range(10):
  time.sleep(0.2)
  sys.stdout.write("%d " % i)
  sys.stdout.flush()
  
sys.stdout.write("\n")  

  
-- 
Grant Edwards                   grante             Yow!  Darling, my ELBOW
                                  at               is FLYING over FRANKFURT,
                               visi.com            Germany...



More information about the Python-list mailing list