Error printing and hibernation in Python

So I have problems with Python. I have a code that:

import time

def printChat(string, sleepTime):
    for a in string:
        print (a, end="")
        time.sleep(sleepTime)
    print()

When I do printChat ("Hello", 0.1), it should print a letter and wait 0.1 seconds, then print the next one, but instead it will wait 0.5 seconds (since " hello " is 5 characters) and then print hello suddenly. Any idea why this is?

+4
source share
1 answer

You should use:

        print (a, end="", flush=True)

Because console output is buffered line by line.

+6
source

Source: https://habr.com/ru/post/1695022/


All Articles