import sys
from time import sleep
def retroprint(prnt):
for i in range(len(prnt)+1):
# below 2 pattern test
#print(prnt[i-1:i], end="")
print(prnt[i-1:i])
sleep(0.085)
return
retroprint("hello your name")
x = input("")
# x = sys.stdin.readline()
retroprint("hello your age")
y = input("")
# y = sys.stdin.readline().rstrip()
There are two ways to print in retroprint, one commented out
Switching to commented out “non-breaking stuff”
It doesn’t move as expected, and unlike when there is a line break, it is not displayed slowly in order
Any ideas?
I don’t think this code will solve the problem of the person who asked the question.
At first, I was researching decorations, but I stopped because it was difficult.
The reason the commented out code prints the whole line in one swoop without delays between the letters, is buffering. If you start python with environment variable PYTHONUNBUFFERED=1, or run python -u then the commented out code will also show the delays.
Alternatively, after each print statement, you could add sys.stdout.flush() which flushes the output line buffer to stdout.
But most simple is to use print(s, end="", flush=True) - which will also immediately write each letter to stdout.
The problem that people have is usual the reverse, but the underlying issue is the same. See the explanation in this StackOverflow post:
Thanks hans
I was looking up the -u option to see if it was related, but
Due to my erroneous judgment, I discarded it from the consideration because it "seems to have nothing to do with the problem."
I’d like to try using flush=True
I will also try to read the provided link
from time import sleep
def retroprint(prnt):
for i in range(len(prnt)+1):
# below 2 pattern test
print(prnt[i-1:i], end="", flush=True)
#print(prnt[i-1:i])
sleep(0.035)
return
txa = "WHAT IS YOUR NAME? "
txb = "HOW OLD ARE YOU? "
spa = " "
spb = " "
retroprint(txa)
x = input("")
# for windows maybe need colorama (ANSIescape)
print(f"\033[A{txa+spa+x+spb}", end="")
retroprint(txb)
y = input("")
print(f"Name:{x} Age:{y}\n\nWelcome Fighter {x}!\n")