r/learnpython • u/IngenuityMission5367 • Oct 02 '26
Slow print for text-based game
I am trying to make a text-based adventure game in Python, how do I make it so the text is typed out slowly and not instantly? I've tried ways online, but they keep returning None at the end. How do I fix this?
0
Upvotes
View all comments
0
u/zaphodikus Oct 02 '26 edited Oct 02 '26
This is perhaps bit advanced for what you are after, but if you paste in this code and call this function, it will work great.
the values for jitter and
base_delayare quite small, a larger value forbase_delaywill make it type slower. If you want a longer pause at the end of each line, makepause_after_punctslightly larger. Use it like thisBasically the
sys.stdout.write(ch)and theflush()are special in that they bypass the buffering that happens when you call print(), that buffering will defeat the effect we are after. Just using print() per character will end up not giving you the fine control that this function does and gives a much more realistic typing speed effect. This is a useful lesson for the future when you start doing file I/O where all the Python I/O is buffered. Alternatively to usingsys.stdout.flush()you can disable buffering in print() by using the-ucommandline parameter, or by setting then environment variablePYTHONUNBUFFERED, but those are both a bit harder in reality. Happy Pythoning./edit fixed indentation.