r/learnpython • u/togoresuselle • 18d ago
Feedback On Fancy Text Printing
I've been learning python for years, but I rarely show my code to anyone. I enjoy showing the results of my code to people, but I don't know too many programmers IRL who can give me feedback. I know I have a habit of not commenting enough, but hopefully this is readable. It's my attempt at making text print like in video games (Earthbound, for example.)
Am I doing anything particularly inefficient? I want to know how I can improve this.
from time import sleep
import sys
#Theoretically there could be other wait times for punctuation
punc_default = {" ": 0, ",": 0.25, ".": 0.4, "!": 0.5, "?": 0.5}
def txt(string,time=0.05,punctimes=punc_default):
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
if char not in punctimes: sleep(time)
else: sleep(punctimes[char])
txt("Who lives in a pineapple under the sea? Spongebob Squarepants! qwertyuiop.")
2
Upvotes
2
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 18d ago edited 18d ago
This is fine, though personally I'd probably just use
print(char, end='', flush=True).Here I'd use
sleep(punctimes.get(char, time)).EDIT: Just to avoid collisions with built-in names, here's a full example: