xSpartanCx
xSpartanCx

Reputation: 311

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()

Upvotes: 0

Views: 365

Answers (1)

Kirk Strauser
Kirk Strauser

Reputation: 30947

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.

Upvotes: 2

Related Questions