Home > Back-end >  Why does time.sleep not work in this instance?
Why does time.sleep not work in this instance?

Time:05-21

When running this code each letter in the word inputed is supposed to be printed out one after another with a time gap inbetween them. For some reason the code prints the whole word at once after a time period. Can anybody help?

import time

def load_word(word, speed):
    for letter in word:
      print(letter, end = "")
      time.sleep(speed)
      
load_word("hello world", 0.15)

CodePudding user response:

you need to flush the buffers like so

print(letter, end='', flush=True)

CodePudding user response:

Try this:

print(letter, end="", flush=True)
  • Related