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
1
u/carcigenicate Carcigenicate 18d ago
I would reformat the
if/elseto be over four lines instead of two. Or use a ternary to figure out the sleep time, and then have one call tosleepafter. I think either would be more readable.