Терминал Linux выполняет Python Script иначе, чем в режиме ожидания - PullRequest
0 голосов
/ 29 сентября 2018

Итак, я написал программу для печати песен, как старый матричный принтер, символ за символом.Когда я запускаю эту вещь в режиме ожидания, она делает это и работает нормально, но когда я вызываю программу из терминала Linux, текст печатается построчно, а не символ за символом, и это действительно отнимает весь эффект и точкупрограмма.Есть ли что-нибудь, что я могу сделать, чтобы это исправить, или это просто, как Терминал будет работать с ним.Спасибо

#!/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 triumph\nI'm making a note here: HUGE SUCCESS\nIt's hard
to overstate my satisfaction\n\nAperture Science\nWe do what we must because we can
For the good of all of us, except the ones who are dead\n\nBut there's no sense
crying over every mistake\nYou just keep on trying 'till you run out of cake
And the Science gets done\nAnd you make a neat gun\nFor the people who are still alive
\nI'm not even angry\nI'm being so sincere right now\nEven though you broke my
heart and killed me\n\nAnd tore me to pieces\nAnd threw every piece into a fire\nAs
they burned it hurt because I was so happy for you\n\nNow these points of data make
a beautiful line\nAnd we're out of beta, we're releasing on time\nSo I'm GLaD I got
burned\nThink of all the things we learned\nFor the people who are still alive"""
    print ("<<<<<<<<<<<<<<<<<<<<<<< Still Alive >>>>>>>>>>>>>>>>>>>>>>>\n")

elif ans == "2":
    song = """Well, here we are again\nIt’s always such a pleasure
Remember when you tried\nTo kill me twice?\n\nOh, how we laughed and laughed
Except I wasn’t laughing\nUnder the circumstances\nI’ve been shockingly nice
\nYou want your freedom? Take it\nThat’s what I’m counting on\nI used to want you dead, but
Now I only want you gone\n\nShe was a lot like you\n(Maybe not quite as heavy)
Now little Caroline\nIs in here too\n\nOne day they woke me up\nSo I could live forever
It’s such a shame the same\nWill never happen to you\n\nYou’ve got your short, sad life left
That’s what I’m counting on\nI’ll let you get right to it\nNow I only want you gone
\nGoodbye, my only friend\nOh, did you think I meant you?\nThat would be funny
If it weren’t so sad\n\nWell, you have been replaced\nI don’t need anyone now
When I delete you, maybe\nI’ll stop feeling so bad\n\nGo make some new disaster
That’s what I’m counting on\nYou’re someone else’s problem\nNow I only want you gone
\nNow I only want you gone\nNow I only want you gone..."""
    print ("<<<<<<<<<<<<<<<<<<<<<<< Want You Gone >>>>>>>>>>>>>>>>>>>>>>>\n")

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

1 Ответ

0 голосов
/ 29 сентября 2018

Терминал кэширует символы построчно.Вам необходимо промыть:

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