2

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?

2
  • 4
    The output may be buffered, the only way to ensure it prints immediately is to pass flush=True to print. Commented Jan 12, 2022 at 18:27
  • Ah, this works, thank you! Commented Jan 12, 2022 at 18:28

2 Answers 2

2

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:

  1. Get rid of the end parameter in the print() statement, as print() statements implicitly add newlines.
  2. Flush the buffer using sys.stdout.flush():
import sys from time import sleep while True: sleep(1) print("test", end="") sys.stdout.flush() 
  1. Use flush=True in the print() statement.
Sign up to request clarification or add additional context in comments.

3 Comments

Just adding flush=True also works, my question is a duplicate of stackoverflow.com/questions/56896710/…
None of these work running via the vscode tester. Do you know how make them work there?
I'm not sure. I would post a new question if I were you.
1

This is a duplicate of Does "time.sleep()" not work inside a for loop with a print function using the "end" attribute?, the output needs to be forced using print("test", end="", flush=True)

1 Comment

If it is a duplicate, then close it as a duplicate. Don't post a link to the duplicate as an answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.