I have started learning Python 2.7.x with the "Learn Python the Hard Way" book. I am currently learning about the raw_input
function and I'm experimenting with different ways to use it. I wrote the following code:
name = raw_input("What is your name? ")
print "Hi %s," % name,
home = raw_input("where do you live? ")
print "I hear that %s is a great place to raise a family, %s." % (home, name)
age = raw_input("How old are you, %s? ") % name
I receive this error with the last line:
TypeError: not all arguments converted during string formatting
How can I use the raw_input
function in a similar way and insert a variable so customize the question embedded in the raw_input
query (apologies if I am making a mess of the terminology)?
Ideally, I'd like to output a question along these lines:
How old are you, Bob?
try:
age = raw_input("How old are you, %s? " % name)
Explanation:
raw_input([prompt])
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
So, when you do
age = raw_input("How old are you, %s? ") % name
let's say you entered Paul
so the above statement becomes,
age = "Paul" % name
and since string "Paul" is not a placeholder it throws the corresponding error.