Skip to content
← Back to Community
Python print on the same line with a delay
Profile icon
panniu

So I was trying to make a game and I used this function:

import time, random def typer(string): string = list(string) for char in string: time.sleep(0.1) print(char, end="")

This instead printed everything at the same time without a delay for every letter.
I used python shell and made sure my code was wrong and it worked fine.

Please fix this bug.
Thanks

Voters
Profile icon
CosmicWonderhoo
Profile icon
panniu
Comments
hotnewtop
Profile icon
pyelias

Add , flush=True to the print() and it will work. This is because most terminals (including repl.it's) are line buffered, and will not update what is shown on the screen until a newline is printed. flush=True forcibly updates the screen without waiting for a newline.

Profile icon
AlexanderMiles

hi

Profile icon
20Aggett

import sys
from time import sleep

def slowprint(text):
for x in text:
print(x, end='')
sys.stdout.flush()
sleep(0.1)

#try this