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

Thursday, September 15, 2022

[FIXED] Why doesn't time.sleep work as with print end arguments?

 September 15, 2022     printing, python, time     No comments   

Issue

I am trying to use time.sleep() to pause in between print statements.

import time
def test():
    print("something", end="")
    time.sleep(1)
    print(" and ", end="")
    time.sleep(1)
    print("something")

When I use the end argument, the code waits before it starts to print. It should print, wait, print, wait, and print ("something, wait, and, wait, something"). However, when I use the end argument, it waits, and prints "wait something and something". This code works as I wanted it to without the end argumets.


Solution

Your output device is line-buffered and only flushes when a new line is output. Add flush=True as an additional parameter to print to force a flush to the output.



Answered By - Mark Tolonen
Answer Checked By - Dawn Plyler (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