String Formatting part 2


#1

name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is %s%s%s, your quest is %s%s%s, " \
"and your favorite color is %s%s%s." % ("name", "quest", "color")

I had written this python code. but my output is as follows.

What is your name? hari
What is your quest? good
What is your favorite color? blue
Traceback (most recent call last):
File "python", line 6, in
TypeError: not enough arguments for format string


#2

you only need one %s to represent a string:

example = "example"
print "to demonstrate %s" % (example)

you have 9 %s and only 3 things to fill it. Hm... but unbalanced.

also, here:

 ("name", "quest", "color")

i would turn those into variable (remove the quotation marks), then they show what you enter in the inputs, otherwise they are just strings


#3

hi
Thanks for your help. I got it, But in the instructions it showed that we have to assign 3 %s.
is it correct


#4

yes, you should have 3 %s, but you have 9 of them.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.