Python - CodeAcademy Madlibs 练习

Python - CodeAcademy Madlibs Exercise

此代码用于 Code Academy 的 Madlibs 练习,它应该从用户那里获取一些输入,然后打印一个非常有趣的输出。

网站已经提供了一个故事,我没有修改它。

当我 运行 脚本时,出现以下错误:

Traceback (most recent call l ast):
File "Madlibs.py", line 30, in
print "This morning I woke up and felt %s because _ was going to finally %s over the big _ %s. On the other side of the %s were many %ss protesting to keep %s in stores. The crowd began to _ to the rythym of the %s, which made all of the %ss very _. %s tried to _ into the sewers and found %s rats. Needing help, %s quickly called %s. %s appeared and saved %s by flying to %s and dropping _ into a puddle of %s. %s then fell asleep and woke up in the year _, in a world where %ss ruled the world." %(a1, name, v1, a2,n1, n2, Animal, Food, v2, n3,Fruit, a3, name, v3, Number,name, Superhero, Superhero, name, Country, name, Dessert,name, Year, n4)

TypeError: not all arguments converted during string formatting

输出还打印 "print" !!!

请注意,在故事中,有些地方我们使用“%ss”而不是“%s”。 Codeacademy 希望用户使用“%ss”(我相信如此)

此外,我尝试用“%s”替换“%ss”- 我遇到了同样的错误。

用“%r”和“%rr”替换了所有“%s”和“%ss”,但出现同样的错误。

自始至终将“%rr”替换为“%r”,但出现同样的错误。

还有一件事我必须提一下,代码要求用户正确地输入所有 raw_input,但无法替换故事中的那些,并且只打印 %s 或 %r 以及错误信息。

有人可以帮助我吗?我的代码对我来说似乎没问题,但我不明白错误消息是怎么回事。

代码如下(重复部分请多多包涵)

# This is a story for Mad Libs !!!

print "Mad Lib is staritng now."

name = raw_input ("What's your name ?")

a1 = raw_input ("How are you feeling today ?")
a2 = raw_input ("How is ther weather?")
a3 = raw_input("Would you like your coffee hot or cold?")

v1 = raw_input ("Would you rather jump, run or walk ?")
v2 = raw_input ("Would you rather sing, dance or act ?")
v3 = raw_input ("Would you rather eat, sleep or watch ?")

n1 = raw_input ("In which city do you live ?")
n2 = raw_input ("What is your favourite pet ?")
n3 = raw_input ("Would you like to go to a mountain or a beach ?")
n4 = raw_input ("DO you wnat to buy a dress or a shoe? ")

Animal = raw_input ("Which animal do you like the most ?")
Food = raw_input ("Enter your favourite food")
Fruit = raw_input ("What's your favourite fruit ?")
Number = raw_input ("Tell me a number: ")
Superhero = raw_input ("Tell me the name of one Superhero")
Country = raw_input ("Which country would you like to visit on your next vacation ?")
Dessert = raw_input ("Which is your favourite dessert ?")
Year = raw_input ("Which year were you born ?")


print "This morning I woke up and felt %s because _ was going to finally %s over the big _ %s. On the other side of the %s were many %ss protesting to keep %s in stores. The crowd began to _ to the rythym of the %s, which made all of the %ss very _. %s tried to _ into the sewers and found %s rats. Needing help, %s quickly called %s. %s appeared and saved %s by flying to %s and dropping _ into a puddle of %s. %s then fell asleep and woke up in the year _, in a world where %ss ruled the world." %(a1, name, v1, a2, n1, n2, Animal, Food, v2, n3, Fruit, a3, name, v3, Number, name, Superhero, Superhero, name, Country, name, Dessert, name, Year, n4)

您的元组中的参数((a1,name......) 参数)比字符串中的 %s 多得多。确保每个参数都恰好与 1 %s 匹配,以使字符串格式正常工作。

link提供了一个解决方案。阅读底部。看来您需要将所有的 _ 替换为 %s。

所以例如

print "This morning I woke up and felt %s because _ was going to finally %s over the big _ %s. On the other side of the %s were many %ss protesting to keep %s in stores. The crowd began to _ to the rythym of the %s, which made all of the %ss very _. %s tried to _ into the sewers and found %s rats. Needing help, %s quickly called %s. %s appeared and saved %s by flying to %s and dropping _ into a puddle of %s. %s then fell asleep and woke up in the year _, in a world where %ss ruled the world." %(a1, name, v1, a2, n1, n2, Animal, Food, v2, n3, Fruit, a3, name, v3, Number, name, Superhero, Superhero, name, Country, name, Dessert, name, Year, n4)

应该是

print "This morning I woke up and felt %s because %s was going to finally %s over the big %s %s. On the other side of the %s were many %s protesting to keep %s in stores. The crowd began to %s to the rythym of the %s, which made all of the %s very %s. %s tried to %s into the sewers and found %s rats. Needing help, %s quickly called %s. %s appeared and saved %s by flying to %s and dropping %s into a puddle of %s. %s then fell asleep and woke up in the year %s, in a world where %s ruled the world." %(a1, name, v1, a2, n1, n2, Animal, Food, v2, n3, Fruit, a3, name, v3, Number, name, Superhero, Superhero, name, Country, name, Dessert, name, Year, n4)