这段代码用于code的Madlibs练习,它应该从用户那里获取一些输入,然后打印一个非常有趣的输出。
有一个故事已经提供在网站上,我没有修改它。
当我运行这个脚本时,我会得到以下错误:
回溯(最近一次呼叫most): 文件"Madlibs.py",第30行 打印:“今天早上我醒来,感觉到%s,因为_s最终会超过大%s,而在%s的另一边,有许多%ss抗议将%s保存在商店中。人群开始向%s的方向移动,这使得所有的%ss都变得非常_。%s试图进入下水道,发现%s鼠。在需要帮助的情况下,%s迅速出现并保存了%s,方法是飞到%s并将%s. %s扔到%s. s的水坑中,然后在_年睡着并醒来,在一个%ss统治世界的世界中醒来。“%s (a1,name,v1,a2,n1,n2,动物,食品,v2,n3,水果,a3,name,v3,Number,name,超级英雄,超级英雄,名字,国家,名称,甜点,名称,年份,n4) TypeError:不是所有在字符串格式化过程中转换的参数。
输出也打印“打印”!
请注意,在故事中,有一些地方我们有'%ss‘而不是'%s’。Codeacademy希望用户使用'%ss‘(我想是的)
此外,我尝试用'%s‘替换’'%ss‘’-我得到了相同的错误。
将所有‘’%1!‘’和‘%1!’‘替换为'%r’和‘’%1!‘’,并得到相同的错误。
在整个过程中将'%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)发布于 2016-07-28 13:58:40
这个链接提供了一个解决方案。往下读。似乎您需要用%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)发布于 2016-07-28 13:57:55
元组中还有许多参数( (a1,name.)参数),而不是字符串中有%s。确保每个参数与字符串格式的精确1 %s匹配才能工作。
https://stackoverflow.com/questions/38638429
复制相似问题