PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, September 17, 2022

[FIXED] Why doesn't print() work in Visual Studio Code?

 September 17, 2022     loops, printing, python, visual-studio-code     No comments   

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:

  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.


Answered By - BrokenBenchmark
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing