Skip to content
Advertisement

Linux Terminal executing Python Script differently to Idle

So I wrote a program to print songs like an old dot matrix printer, character by character. When I run the thing in Idle it does it and works fine, but when I call the program from the Linux Terminal the text is printed line by line, instead of character by character, and it really takes away from the whole effect and point of the program. Is there anything I can do to fix this or is it just how the Terminal will work with it. Thanks

#!/usr/bin/python3
import time

print ("<<<<<<<<<<<<<<<<<<<<<<< Portal Song Printer >>>>>>>>>>>>>>>>>>>>>>>")
ans = input("Still Alive(1) or Want You Gone(2):n")
if ans == "1":
    song = """This was a triumphnI'm making a note here: HUGE SUCCESSnIt's hard
to overstate my satisfactionnnAperture SciencenWe do what we must because we can
For the good of all of us, except the ones who are deadnnBut there's no sense
crying over every mistakenYou just keep on trying 'till you run out of cake
And the Science gets donenAnd you make a neat gunnFor the people who are still alive
nI'm not even angrynI'm being so sincere right nownEven though you broke my
heart and killed mennAnd tore me to piecesnAnd threw every piece into a firenAs
they burned it hurt because I was so happy for younnNow these points of data make
a beautiful linenAnd we're out of beta, we're releasing on timenSo I'm GLaD I got
burnednThink of all the things we learnednFor the people who are still alive"""
    print ("<<<<<<<<<<<<<<<<<<<<<<< Still Alive >>>>>>>>>>>>>>>>>>>>>>>n")

elif ans == "2":
    song = """Well, here we are againnIt’s always such a pleasure
Remember when you triednTo kill me twice?nnOh, how we laughed and laughed
Except I wasn’t laughingnUnder the circumstancesnI’ve been shockingly nice
nYou want your freedom? Take itnThat’s what I’m counting onnI used to want you dead, but
Now I only want you gonennShe was a lot like youn(Maybe not quite as heavy)
Now little CarolinenIs in here toonnOne day they woke me upnSo I could live forever
It’s such a shame the samenWill never happen to younnYou’ve got your short, sad life left
That’s what I’m counting onnI’ll let you get right to itnNow I only want you gone
nGoodbye, my only friendnOh, did you think I meant you?nThat would be funny
If it weren’t so sadnnWell, you have been replacednI don’t need anyone now
When I delete you, maybenI’ll stop feeling so badnnGo make some new disaster
That’s what I’m counting onnYou’re someone else’s problemnNow I only want you gone
nNow I only want you gonenNow I only want you gone..."""
    print ("<<<<<<<<<<<<<<<<<<<<<<< Want You Gone >>>>>>>>>>>>>>>>>>>>>>>n")

for c in song:
    print(c, end="")
    time.sleep(.05)

Advertisement

Answer

The terminal caches characters by line. You need to flush:

import sys
for c in song:
    print(c, end="")
    sys.stdout.flush()
    time.sleep(.05)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement