Using% r in raw_input in python

Is it possible to use% r in raw_input in python?

In context, I'm working on Zed Shaw Exercise 12 . (Great resource! The lessons are very useful and well developed.)

I play in extra credit, trying to get raw_input to repeat what I typed. I know I can do the same with a print expression, but I'm curious if I can execute it in raw_input.

The code I am typing is:

from sys import argv script, firstname, lastname = argv age = raw_input("Hello %r, what is your age? ") % firstname print ("Ahh, Mr. %r, you are %r years old.") % (lastname, age) 

The error I am getting is:

 Traceback (most recent call last): File "ex13a.py", line 5, in <module> age = raw_input("Hello %r, what is your age? ") % firstname TypeError: not all arguments converted during string formatting 

Thanks in advance!

+4
source share
1 answer

Your line should read

 raw_input("Hello %r, what is your age? " % firstname) 

instead

 raw_input("Hello %r, what is your age? ") % firstname 

Otherwise, you would not format the string "Hello %r, ..." , but the resulting call string raw_input .

+7
source

All Articles