Forums

First raw_input() stopping preceding string from printing?

I'm working my way through Learn Python the Hard Way using PythonAnywhere (as I am unable to install Python on my work laptop), and have reached exercise 11 where the code for the exercise is as follows:

print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()

print "So, you're %r old, %r tall and %r heavy." % (
age, height, weight)

When I run it, the first question doesn't appear - I just get a blank prompt. If I enter data and hit "return", I then get the second question just fine. However, if I comment out the "age = raw_input()" line then the second question doesn't appear either - but the third does.

Is this a bug, or am I overlooking something obvious?

Frankly, I am baffled. That raw_input() seems to be clearing the line before it. I have no idea why. Something to do with flushing or stdout buffering or something... Any people wiser than I have any ideas? For info, we send an ANSI escape code to clear the screen during console load (\x1b[;H\x1b[2J), that might have anything to do with it.

@john, if you just want to get past it and carry on with the book -- i don't think it's something you need to worry about at this stage -- then, just remove that trailing comma from the first print line...

@johnthelutheran: This won't solve the issue for someone that really needs to make it work as designed, however if you just want to see code run and get a peek at Python 3 syntax at the same time, consider running the code below in place of your Python 2 code. I made a few tweaks, so if you have any questions either play with it to see what is happening or please ask and answers will appear.

#!/usr/local/bin/python3.3
age = input("How old are you? ")
height = input("How tall are you? ")
weight = input("How much do you weigh? ")
print("So, you're {} old {} tall and {} heavy.".format(age, height, weight))

Thanks! The next chapter in LPTHW gives a similar approach.

Hadn't been letting the issue hold me up: just wanted to flag it up in case it was a bug in PA (perhaps not picked up on precisely because it's not the way anyone would code that stuff if they've got past chapter 13!).

YW. I almost didn't write it as I didn't want to offend you. Then I figured, eh, you are on a learning path, so the more you are exposed to that doesn't hurt you the better...☺

~a2j