此代码适用于Code Academy的Madlibs练习,该练习应该从用户那里获取大量输入,然后打印出非常有趣的输出。
网站上已经提供了一个故事,我没有对其进行修改。
当我运行脚本时,我收到以下错误:
追踪(最近的呼叫l AST):
文件“Madlibs.py”,第30行, 在 打印“今天早上我醒来并感觉到%s因为_最终会超过%_%s。在%s的另一边是很多%ss抗议在商店里保留%s。人群开始_%s的rythym,这使得所有%ss非常_%s试图_进入下水道并找到%s的老鼠。需要帮助,%s迅速称为%s。%s出现并保存%s飞行到%s并将_放入%s。%s的水坑然后睡着了,并在这一年中醒来_,在一个%ss统治世界的世界里。 %(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:并非在字符串格式化期间转换所有参数
输出还会打印“print”!!!
请注意,在故事中,有些地方我们有'%ss'而不是'%s'。 Codeacademy希望用户使用'%ss'(我相信如此)
另外,我尝试用'%s'替换'%ss' - 我得到了同样的错误。
将所有'%s'和'%ss'替换为'%r'和'%rr'并得到相同的错误。
将'%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)
答案 0 :(得分:0)
你的元组中有更多的参数((a1,name ......))而不是你的字符串中有%s。确保每个参数与1%s完全匹配,以使字符串格式化起作用。
答案 1 :(得分:0)
此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)