Simple (?) question about print statement
Fredrik Lundh
fredrik at pythonware.com
Wed Dec 14 16:04:55 EST 2005
"TY" <aqteon at yahoo.com> wrote:
> I have this little simple script:
>
> for i in range(10):
> for j in range(5000000): pass # Timing-delay loop
> print i
>
> When you run it, it behaves as you would expect -- it prints 0 <pause>
> on the next line prints 1 <pause> on the next line prints 2 <pause>
> etc.
>
> But if you add a comma at the end of print statement on the last line
> like this:
>
> 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?
because stdout is line buffered.
try this instead:
import time, sys
for i in range(10):
time.sleep(0.8) # seconds; tune as necessary
print i,
sys.stdout.flush() # flush stdout
print
</F>
More information about the Python-list
mailing list