2016-08-15 1 views
-2

Also ich versuche, eine einfache Mad Libs-Programm zu machen, und ich bekomme mehrere Fehler und ich kann nicht herausfinden, warum. Einer von ihnen ist mit dem "Noah's First Program" -Teil und der andere ist mit dem Drucken der Story-Variable. Wie repariere ich es?Python Syntax Fehler auf Mad libs

print "Noah's First Program!" 

name=raw_input("Enter a name:") 
adjectiveone=raw_input("Enter an Adjective:") 
adjectivetwo=raw_input("Enter an Adjective:") 
adjectivethree=raw_input("Enter an Adjective:") 
verbone=raw_input("Enter a Verb:") 
verbtwo=raw_input("Enter a Verb:") 
verbthree=raw_input("Enter a Verb:") 
nounone=raw_input("Enter a Noun:") 
nountwo=raw_input("Enter a Noun:") 
nounthree=raw_input("Enter a Noun:") 
nounfour=raw_input("Enter a Noun:") 
animal=raw_input("Enter an Animal:") 
food=raw_input("Enter a Food:") 
fruit=raw_input("Enter a Fruit:") 
number=raw_input("Enter a Number:") 
superhero=raw_input("Enter a Superhero Name:") 
country=raw_input("Enter a Country:") 
dessert=raw_input("Enter a Dessert:") 
year=raw_input("Enter a Year:") 

STORY = “Man, I look really %s this morning. My name is %s, by the   way, and my favorite thing to do is %s. My best friend is super %s, because he owns a(n) %s and a(n) %s! What’s your favorite animal? Mine is a %s. I like to watch them at the zoo as I eat %s while %s. Those things are all great, but my other friend is even more interesting! She has a %s, and a lifetime supply of %s! She’s really %s, and her name is %s. She enjoys %s, but only %s times per day! She usually does it with %s. My favorite superhero is %s, but hers is %s. My third friend is named %s and is foreign. His family comes from %s, and their family name is %s. To wrap things up, my favorite dessert is %s, and I’m glad to have introduced you to my friends. Maybe soon I’ll introduce you to my fourth friend %s, but that will probably be in the year %s! I love %s!" 

print STORY (adjectiveone,name,verbone,adjectivetwo,nounone,nountwo,animal,food,verbtwo,nounthree,fruit,adjectivethree,name,verbthree,number,name,superhero,superhero,name,country,name,dessert,name,year,nounfour) 
+3

Was ist die Fehlermeldung? Welche Version von Python hast du installiert? –

+0

Müssen Sie vielleicht den Apostrophen entkommen? –

+0

Ich sehe geschweifte Anführungszeichen. Hast du einen Teil davon in Word geschrieben oder so? – user2357112

Antwort

-1

Es scheint ein Python 2.7-Programm zu sein. Wenn Sie Python 2.7 verwenden, erhalten Sie Syntaxfehler, weil print keine Anweisung, sondern eine Funktion in Python 3 ist: Sie benötigen Klammern.

Falsch:

print "Noah's First Program!" 

Gut:

print("Noah's First Program!") 
+0

Danke, das hat diesen Teil behoben, aber es hat immer noch einen Syntaxfehler auf der Druckgeschichte Aspekt. –

+0

Fehlendes Komma nach "STORY": benutze 'print (STORY, (Adjektiv, ...))'. –

1

Wenn Sie Python verwenden 2, die raw_input führt mich zu glauben, sollten Sie den Code wie folgt:

print "Noah's First Program!" 

name=raw_input("Enter a name:") 
adjectiveone=raw_input("Enter an Adjective:") 
adjectivetwo=raw_input("Enter an Adjective:") 
adjectivethree=raw_input("Enter an Adjective:") 
verbone=raw_input("Enter a Verb:") 
verbtwo=raw_input("Enter a Verb:") 
verbthree=raw_input("Enter a Verb:") 
nounone=raw_input("Enter a Noun:") 
nountwo=raw_input("Enter a Noun:") 
nounthree=raw_input("Enter a Noun:") 
nounfour=raw_input("Enter a Noun:") 
animal=raw_input("Enter an Animal:") 
food=raw_input("Enter a Food:") 
fruit=raw_input("Enter a Fruit:") 
number=raw_input("Enter a Number:") 
superhero=raw_input("Enter a Superhero Name:") 
country=raw_input("Enter a Country:") 
dessert=raw_input("Enter a Dessert:") 
year=raw_input("Enter a Year:") 

STORY = "Man, I look really %s this morning. My name is %s, by the way, and my favorite thing to do is %s. My best friend is super %s, because he owns a(n) %s and a(n) %s! What's your favorite animal? Mine is a %s. I like to watch them at the zoo as I eat %s while %s. Those things are all great, but my other friend is even more interesting! She has a %s, and a lifetime supply of %s! She's really %s, and her name is %s. She enjoys %s, but only %s times per day! She usually does it with %s. My favorite superhero is %s, but hers is %s. My third friend is named %s and is foreign. His family comes from %s, and their family name is %s. To wrap things up, my favorite dessert is %s, and I'm glad to have introduced you to my friends. Maybe soon I'll introduce you to my fourth friend %s, but that will probably be in the year %s! I love %s!" 

print STORY % (adjectiveone,name,verbone,adjectivetwo,nounone,nountwo,animal,food,verbtwo,nounthree,fruit,adjectivethree,name,verbthree,number,name,superhero,superhero,name,country,name,dessert,name,year,nounfour) 

Zusammenfassend:

Ersetzen Sie Ihre Apostrophe durch '.

Fix Ihre Syntax für String-Formatierung:

print STORY(arg1, ..., argn) 

sein sollte:

print STORY % (arg1, ..., argn) 

Wenn Sie 3 Python verwenden, ersetzen raw_input mit input und print ... mit print(...). Auch nach pep-8, sollten Sie einen einzigen Raum haben auf beiden Seiten der = wenn Variablen zuweisen, so zum Beispiel:

name=raw_input("Enter a name:") 

sollten sein:

name = raw_input("Enter a name:") 

Obwohl es nicht auf diese Weise tun wird verursacht keinen Syntaxfehler.

+0

Okay, also vielen Dank, das hat funktioniert, außer dass es einen neuen Fehler bekommen hat. So sah es aus: Noahs erstes Programm! Geben Sie einen Namen: Noah Traceback (jüngste Aufforderung zuletzt): Datei "/ Users/Noah/Desktop/Mad Libs 1.py", Zeile 3, in name = raw_input ("Geben Sie einen Namen:") EOFError: EOF beim Lesen einer Zeile –

+0

hmm ... Wie läuft dein Skript? Versuche, eine Befehlszeile/ein Terminal zu öffnen und folgendes einzugeben: 'python/Benutzer/Noah/Desktop/Mad \ Libs \ 1.py'. Außerdem ist es am besten, keine Leerzeichen in den Skriptnamen zu haben. Die übliche Python-Methode, um dieses Skript zu benennen, wäre 'madlibs.py' oder' mad_libs.py' – elethan