Python - CodeAcademy Madlibs练习

问题描述:

此代码适用于Code Academy的Madlibs练习,该练习应该从用户处获取大量输入,然后打印出非常有趣的输出。Python - CodeAcademy Madlibs练习

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

当我运行该脚本,我收到以下错误:

............................. ..............................................

回溯(最近通话升 AST):
文件“Madlibs.py”,30日线 在
打印“今天早上我醒来的时候,觉得%是因为_是要最终%S在大_% s。在%s的另一边有很多%s*保持%s在商店里,人群开始到%s的节奏,这使%s的所有%非常_。 %s试图进入下水道并发现%s的老鼠。需要帮助,%s很快称为%s。 %s出现并通过飞行到%s并将_放入%s的水坑中而保存了%s。然后%s在这个年份里睡着了,并且在一个世界上%s统治世界的世界里醒来。“%(a1,name,v1,a2,n1,n2,Animal,Food,v2,n3,Fruit,a3,名,V3,编号,姓名,超级英雄,超级英雄,姓名,国籍,名字,甜品,名称,年份,N4)

类型错误:无法在字符串格式化
转换所有参数......... .................................................. ................

输出还打印出“打印”!

请注意,在故事中,也有一些地方,我们有' %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) 

这个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) 
+0

谢谢!!!!这解决了问题:) – Sarat

您的元组(在(A1,名字......)参数)比你更多的参数在你的字符串中有%s。确保每个参数匹配恰好为1%,以使字符串格式化工作。

+0

我算过,他们都匹配的参数和%s号和“思想”。但是,如上所示,我将代码中的所有'_'替换为'%s',并且它工作正常。谢谢:) – Sarat