2016-07-28 4 views
1

このコードは、コード・アカデミーのMadlibのエクササイズ用です。ユーザーからの入力を多数受け取り、非常に面白い出力をプリントすることになっています。Python - CodeAcademy Madlibs Exercise

サイト内に既にストーリーがあり、私はそれを変更していません。私は、スクリプトを実行すると

、私は次のエラーを取得しています:

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

トレースバック(最新の呼び出しリットル AST):_は_%大きい上、最終的%sのつもりだったので、
ファイル「Madlibs.py」、ライン30、
での印刷は、「今朝は目が覚めたと%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)

例外TypeError:文字列が
のフォーマット時に変換されていないすべての引数......... .................................................. ................

出力も「プリント」を印刷します!!!

物語の中で、私たちは持っているいくつかの場所が「あることに注意してください%s 'の代わりに'%ss 'を使用します。Codeacademyはユーザーに'%ss 'を使用させたいと考えています(私はそう信じています)

また、 '%ss'を '%s'に置き換えようとしました。同じエラーが発生しました。

'%s'と '%ss'はすべて '%r'と '%rr'で置き換えられ、同じエラーが発生しました。

'%rr'を '%r'に置き換えて、同じエラーが発生しました。

私が言及しなければならないもう1つのことは、コードは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

この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) 

の例については、それは私が彼らが一致した引数の数と%sおよび「思想」の両方を数えていた

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

0

あなたはあなたよりもあなたのタプル((A1、名前......)引数)でより多くの引数を持つコードでありますあなたの文字列に%sがあります。文字列の書式設定が正しく機能するためには、各引数が正確に1%で一致することを確認してください。

+0

でなければなりません。しかし、上のように、コード内の '_'をすべて '%s'に置き換えて動作させました。ありがとう:) – Sarat

関連する問題