Issue
I'm trying to make a while True
-loop that repeatedly prints a string:
from time import sleep
while True:
sleep(1)
print("test", end="")
But it doesn't print anything when running it with VSC. Running it with the IDLE works for me, a friend also tried it and for him, it's the other way round.
Why does this happen?
Solution
Python's stdout
is buffered, meaning that prints to stdout
don't appear on the console until a newline is printed, the buffer is full, or if the buffer is flushed.
You have three options:
- Get rid of the end parameter in the
print()
statement, asprint()
statements implicitly add newlines. - Flush the buffer using
sys.stdout.flush()
:
import sys
from time import sleep
while True:
sleep(1)
print("test", end="")
sys.stdout.flush()
- Use
flush=True
in theprint()
statement.
Answered By - BrokenBenchmark Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.