此代碼適用於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)
謝謝!!!!這解決了問題:) – Sarat