Python不支持的格式字符“w”

Python不支持的格式字符“w”,python,Python,我正在创建一个madlibs游戏,它接受用户输入并返回故事。我使用%s存储输入。当我收到此错误时,我根据以前的帖子将%s更改为%%s,但错误仍然存在 print "What is your name?" a = raw_input() print "Hello", a, "Lets see if you like you can do MadLibs!" print "HEAD KNIGHT OF NI: %s \n KNIGHTS OF NI : %s \n ARTHUR : Who are

我正在创建一个madlibs游戏,它接受用户输入并返回故事。我使用%s存储输入。当我收到此错误时,我根据以前的帖子将%s更改为%%s,但错误仍然存在

print "What is your name?"
a = raw_input()
print "Hello", a, "Lets see if you like you can do MadLibs!"
print "HEAD KNIGHT OF NI: %s \n KNIGHTS OF NI : %s \n ARTHUR : Who are you ? \n HEAD KNIGHT : We are the %s who say. %s \n RANDOM : %s \n ARTHUR : No ! Not the %s who say %s \n Head Knight: The Same! \n Bedevere: Who are they? \n Head Knight: We are the keepers of the %s words : %%s %%s and %%s \n Random: %%s \n Arthur: Those who %s them seldom live to tell the tale. \n Head Knight: The %%s who say %%s demand a sacrifice \n Arthur: %s of %s we are but %s travellers who seek the % who lives beyong these woods \n Head Knight: %s \n Knights of Ni: %s \n Arthur: Ow Agh \n Head Knight: We shall say %s again to you if you don't appease us \n Arthur: Well what is it you want? \n Head Knight: We want a %%s \n Arthur: A what? \n Knights of Ni: %%s \n Arthur and Party: OWWW, AHHH \n Arthur: Please, No More! We will find you a %%s \n Head Knight: You must return here with a %%s or else, you will never pass through these woods alive. \n Arthur: O %%s of %%s you are %%s and %%s and we will return with a %%s. \n Head Knight: One that looks %%s \n Arthur: Of Course \n Head Knight: and not too %%s \n Arthur: Yes" % (raw_input("Nonsense Word"), raw_input("Nonsense Word "), raw_input("Occupation"),raw_input("Nonsense Word"),
                       raw_input("Nonsense Word"), raw_input("Occupation"), raw_input("Nonsense Word"), raw_input("Adjective"), raw_input("Nonsense Word"),
错误为ValueError:索引466处不支持的格式字符“w”0x77

您的字符串中有文字%字符;将其加倍以防止Python尝试将其解释为格式字符串:

"... very long string ... who seek the %% who lives beyong ..."
或者你错过了那里的一个s:

"... very long string ... who seek the %s who lives beyong ..."
你真的想看看tripple引用你的字符串。Python让您用作字符串分隔符,并且在这种文本字符串中保留换行符:

MADLIBS = """\
HEAD KNIGHT OF NI: %s 
 KNIGHTS OF NI : %s 
 ARTHUR : Who are you ? 
 HEAD KNIGHT : We are the %s who say. %s 
 RANDOM : %s 
 ARTHUR : No ! Not the %s who say %s 
 Head Knight: The Same! 
 Bedevere: Who are they? 
 Head Knight: We are the keepers of the %s words : %%s %%s and %%s 
 Random: %%s 
 Arthur: Those who %s them seldom live to tell the tale. 
 Head Knight: The %%s who say %%s demand a sacrifice 
 Arthur: %s of %s we are but %s travellers who seek the %% who lives beyong these woods 
 Head Knight: %s 
 Knights of Ni: %s 
 Arthur: Ow Agh 
 Head Knight: We shall say %s again to you if you don't appease us 
 Arthur: Well what is it you want? 
 Head Knight: We want a %%s 
 Arthur: A what? 
 Knights of Ni: %%s 
 Arthur and Party: OWWW, AHHH 
 Arthur: Please, No More! We will find you a %%s 
 Head Knight: You must return here with a %%s or else, you will never pass through these woods alive. 
 Arthur: O %%s of %%s you are %%s and %%s and we will return with a %%s. 
 Head Knight: One that looks %%s 
 Arthur: Of Course 
 Head Knight: and not too %%s 
 Arthur: Yes
"""
这样,您可以更清楚地看到字符串中有几个可能需要修复的加倍百分比标记。

您的字符串中有一个文字百分比字符;将其加倍以防止Python尝试将其解释为格式字符串:

"... very long string ... who seek the %% who lives beyong ..."
或者你错过了那里的一个s:

"... very long string ... who seek the %s who lives beyong ..."
你真的想看看tripple引用你的字符串。Python让您用作字符串分隔符,并且在这种文本字符串中保留换行符:

MADLIBS = """\
HEAD KNIGHT OF NI: %s 
 KNIGHTS OF NI : %s 
 ARTHUR : Who are you ? 
 HEAD KNIGHT : We are the %s who say. %s 
 RANDOM : %s 
 ARTHUR : No ! Not the %s who say %s 
 Head Knight: The Same! 
 Bedevere: Who are they? 
 Head Knight: We are the keepers of the %s words : %%s %%s and %%s 
 Random: %%s 
 Arthur: Those who %s them seldom live to tell the tale. 
 Head Knight: The %%s who say %%s demand a sacrifice 
 Arthur: %s of %s we are but %s travellers who seek the %% who lives beyong these woods 
 Head Knight: %s 
 Knights of Ni: %s 
 Arthur: Ow Agh 
 Head Knight: We shall say %s again to you if you don't appease us 
 Arthur: Well what is it you want? 
 Head Knight: We want a %%s 
 Arthur: A what? 
 Knights of Ni: %%s 
 Arthur and Party: OWWW, AHHH 
 Arthur: Please, No More! We will find you a %%s 
 Head Knight: You must return here with a %%s or else, you will never pass through these woods alive. 
 Arthur: O %%s of %%s you are %%s and %%s and we will return with a %%s. 
 Head Knight: One that looks %%s 
 Arthur: Of Course 
 Head Knight: and not too %%s 
 Arthur: Yes
"""

这样,您就可以更清楚地看到,您的字符串中有几个可能需要修复的加倍百分比标记。

我认为您实际上不想在其中包含任何%%s字符串。而且,如果像ni这样的词在整个过程中保持一致,这就更有意义了。因此,我用键控占位符重写了它:

print "What is your name?"
a = raw_input()
print "Hello", a, "Let's see if you can do MadLibs!"
categories = {  'ni':'Nonsense word','knights':'Occupation',
                'sacred':'Adjective','peng':'Nonsense word',
                'nee-wom':'Nonsense word','hear':'Verb',
                'simple':'Adjective','enchanter':'Occupation',
                'shrubbery':'Noun','just':'Adjective',
                'fair':'Adjective','nice':'Adjective',
                'expensive':'Adjective'}
input_words = {}
for key,description in categories.items():
    input_words[key] = raw_input('Enter a(n) '+description+': ')
print '''HEAD KNIGHT OF NI: %(ni)s 
 KNIGHTS OF NI : %(ni)s 
 ARTHUR : Who are you ? 
 HEAD KNIGHT : We are the %(knights)s who say. %(ni)s 
 RANDOM : %(ni)s 
 ARTHUR : No ! Not the %(knights)s who say %(ni)s 
 Head Knight: The Same! 
 Bedevere: Who are they? 
 Head Knight: We are the keepers of the %(sacred)s words : %(ni)s %(peng)s and %(nee-wom)s 
 Random: %(nee-wom)s 
 Arthur: Those who %(hear)s them seldom live to tell the tale. 
 Head Knight: The %(knights)s who say %(ni)s demand a sacrifice 
 Arthur: %(knights)s of %(ni)s we are but %(simple)s travellers who seek the %(enchanter)s who lives beyond these woods 
 Head Knight: %(ni)s 
 Knights of Ni: %(ni)s 
 Arthur: Ow Agh 
 Head Knight: We shall say %(ni)s again to you if you don't appease us 
 Arthur: Well what is it you want? 
 Head Knight: We want a %(shrubbery)s 
 Arthur: A what? 
 Knights of Ni: %(ni)s 
 Arthur and Party: OWWW, AHHH 
 Arthur: Please, No More! We will find you a %(shrubbery)s 
 Head Knight: You must return here with a %(shrubbery)s or else, you will never pass through these woods alive. 
 Arthur: O %(knights)s of %(ni)s you are %(just)s and %(fair)s and we will return with a %(shrubbery)s. 
 Head Knight: One that looks %(nice)s 
 Arthur: Of Course 
 Head Knight: and not too %(expensive)s 
 Arthur: Yes''' % input_words

我不认为你真的想要任何%%s字符串在那里。而且,如果像ni这样的词在整个过程中保持一致,这就更有意义了。因此,我用键控占位符重写了它:

print "What is your name?"
a = raw_input()
print "Hello", a, "Let's see if you can do MadLibs!"
categories = {  'ni':'Nonsense word','knights':'Occupation',
                'sacred':'Adjective','peng':'Nonsense word',
                'nee-wom':'Nonsense word','hear':'Verb',
                'simple':'Adjective','enchanter':'Occupation',
                'shrubbery':'Noun','just':'Adjective',
                'fair':'Adjective','nice':'Adjective',
                'expensive':'Adjective'}
input_words = {}
for key,description in categories.items():
    input_words[key] = raw_input('Enter a(n) '+description+': ')
print '''HEAD KNIGHT OF NI: %(ni)s 
 KNIGHTS OF NI : %(ni)s 
 ARTHUR : Who are you ? 
 HEAD KNIGHT : We are the %(knights)s who say. %(ni)s 
 RANDOM : %(ni)s 
 ARTHUR : No ! Not the %(knights)s who say %(ni)s 
 Head Knight: The Same! 
 Bedevere: Who are they? 
 Head Knight: We are the keepers of the %(sacred)s words : %(ni)s %(peng)s and %(nee-wom)s 
 Random: %(nee-wom)s 
 Arthur: Those who %(hear)s them seldom live to tell the tale. 
 Head Knight: The %(knights)s who say %(ni)s demand a sacrifice 
 Arthur: %(knights)s of %(ni)s we are but %(simple)s travellers who seek the %(enchanter)s who lives beyond these woods 
 Head Knight: %(ni)s 
 Knights of Ni: %(ni)s 
 Arthur: Ow Agh 
 Head Knight: We shall say %(ni)s again to you if you don't appease us 
 Arthur: Well what is it you want? 
 Head Knight: We want a %(shrubbery)s 
 Arthur: A what? 
 Knights of Ni: %(ni)s 
 Arthur and Party: OWWW, AHHH 
 Arthur: Please, No More! We will find you a %(shrubbery)s 
 Head Knight: You must return here with a %(shrubbery)s or else, you will never pass through these woods alive. 
 Arthur: O %(knights)s of %(ni)s you are %(just)s and %(fair)s and we will return with a %(shrubbery)s. 
 Head Knight: One that looks %(nice)s 
 Arthur: Of Course 
 Head Knight: and not too %(expensive)s 
 Arthur: Yes''' % input_words
你在世界卫生组织面前有一个单独的百分比。此外,你还将beyong拼写为beyong。在who之前,你有一个单独的%。此外,您还将beyond拼写为beyong。