简体   繁体   English

即使在物理循环之前,Python的“打印”也会在while循环之后出现?

[英]Python “print” coming after while loop even though it's physically before?

I'm messing around with some python (in eclipse using dynamic language toolkit) and even though I have the print command a line before my while loop, I have to enter the values and type "done" before it prints everything, then it will print the prompt and the values. 我在弄乱一些python(在使用动态语言工具包的eclipse中),即使我在while循环之前的一行上有print命令,我也必须输入值并键入“ done”,然后才能打印所有内容,然后它将打印提示和值。 I've tried using the raw_input() built in prompt as well to no avail. 我试过使用内置提示中的raw_input()也是无济于事的。 Is this just an issue with the editor or is something wrong with my code? 这仅仅是编辑器的问题,还是我的代码有问题?

class squareNums:
def __init__(self):
    self.nums = []
    self.getInput()

def getInput(self):
    print ('Enter values individually, then type "done" when finished')
    while True:
        var = raw_input()
        if var == 'done':
            break
        else:
            self.nums.append(var)
    print self.nums

squareNums() squareNums()

I think that's a "feature" of the environment you're executing it in. When run from the command line, I see just what you'd expect: 我认为这是您要在其中执行环境的“功能”。从命令行运行时,我看到的正是您所期望的:

$ python /tmp/tester.py
Enter values individually, then type "done" when finished
123
123
132
123
done
['123', '123', '132', '123']

If I had to guess, I'd say that your terminal is buffering all output until the program ends, and then flushing it all to the screen at once. 如果我不得不猜测,我会说您的终端正在缓冲所有输出,直到程序结束,然后立即将其全部刷新到屏幕上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尽管 Python 中没有说明这样的条件,但控制流从 while 循环中出来 - Control flow is coming out of while loop though no such condition is stated in Python 我有一个被忽略的while循环,即使它不应该| Selenium | Python - I have a while loop that's being ignored even though it shouldn't | Selenium | Python Python while while循环永远不会停止,即使它应该 - Python while loop never stops even though it should Python 循环在 99999 之后停止输出,即使它应该继续 - Python loop stops outputting after 99999 even though it should continue python while循环中的while循环在赢得比赛后忽略打印 - python while loop in a while loop ignores the print after winning the game 进入 while 循环,即使它是 false - while loop is entered even though it is false 为什么即使 python 中的条件为真,我的 while 语句的主体也不打印 - Why does does the body of my while statement not print even though the condition is true in python 调用联接后,Python甚至不会打印测试字符串。 虽然执行代码为0 - Python will not print even test strings after calling a join. Executes with code 0 though 为什么即使条件逻辑始终为真,异步任务的 done() 方法上的 while 循环也会中断? - Why the while loop on asyncio task's done() method break even though the condition logic is always true? 即使尝试了,Python循环也会重新启动:例外: - Python loop restarting even though there is a try: except:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM