Linux终端执行Python脚本的方式与空闲不同

Linux终端执行Python脚本的方式与空闲不同,python,linux,terminal,python-idle,Python,Linux,Terminal,Python Idle,所以我写了一个程序,像旧的点阵打印机一样,一个字符一个字符地打印歌曲。当我在空闲状态下运行这个程序时,它运行正常,但当我从Linux终端调用程序时,文本是逐行打印的,而不是逐字符打印的,这确实会影响程序的整体效果和要点。 我能做些什么来解决这个问题,或者这就是终端将如何使用它。谢谢 #!/usr/bin/python3 import time print ("<<<<<<<<<<<<<<<<<

所以我写了一个程序,像旧的点阵打印机一样,一个字符一个字符地打印歌曲。当我在空闲状态下运行这个程序时,它运行正常,但当我从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)
#/usr/bin/python3
导入时间
打印(“>”)
ans=输入(“仍然活着(1)或希望你离开(2):\n”)
如果ans==“1”:
song=“”这是一次胜利\n我在这里记下:巨大的成功\n这很难
为了夸大我的满意度\n\n我们做了我们必须做的事情,因为我们可以
为了我们所有人的利益,除了那些已经死去的人\n\n但是没有意义
为每一个错误哭泣\n你只要继续尝试,直到你的蛋糕用完为止
科学已经完成,你为活着的人做了一把漂亮的枪
\我甚至没有生气\n我现在很真诚\n即使你打破了我的承诺
我的心和我的生命\n\n将我撕成碎片\n并将每一块都扔进火中\n
他们把它烧得很痛,因为我为你感到高兴\n\n这些数据说明
一条漂亮的线路\n我们已经过了测试版,我们正在按时发布\n所以我很高兴我得到了
烧掉了\n我们为活着的人学到的所有东西的记忆“”
打印(“>\n”)
elif ans==“2”:
song=“”好吧,我们又来了\n真是太高兴了
还记得你想杀我两次吗?\n\n不,我们笑了又笑
除了我没有笑\n在这种情况下\n我一直非常好
\你想要你的自由?拿着它\n这是我指望的\n我曾经想要你死,但是
现在我只想让你走\n\n她和你很像\n(也许没那么重)
现在小卡罗琳\n也在这里\n \n没有一天他们叫醒了我\n这样我就可以永远活下去了
太遗憾了,同样的事情\n永远不会发生在你身上\n\n你的短暂而悲伤的生活已经结束了
这就是我所指望的\n我会让你马上去做\n现在我只想让你走
\nGoodbye,我唯一的朋友\n哦,你以为我是说你吗?\n那会很有趣
如果不是那么伤心的话\n\n那么,你已经被替换了\n我现在不需要任何人了
当我删除你时,也许\n我将不再感觉如此糟糕\n\n造成新的灾难
这就是我指望的\n你是别人的问题\n现在我只想让你走
\现在我只想你走\n现在我只想你走……”“
打印(“>\n”)
对于歌曲中的c:
打印(c,end=“”)
时间。睡眠(.05)

终端按行缓存字符。您需要冲洗:

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

终端不执行python代码。相反,它显示程序的输出。因此,更好的标题可能是“Linux终端显示的输出与IDLE的Shell不同”。此外,我怀疑有不止一个linux终端程序,至少有一些可以配置为使用行缓冲或不使用。在Unix衍生产品上,可以使用诅咒来控制终端设置。raw()和cbreak()模式关闭行缓冲,至少对于输入。