简体   繁体   中英

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. I've tried using the raw_input() built in prompt as well to no avail. 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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM