我正在尝试用python制作一个madlibs程序,但我不确定是什么导致它出错



我对编程非常陌生,所以如果这是一个新手错误,我深表歉意。

#import statements
#function definitions
useradj1=input(str("Please enter an adjective. ")) #asks for adjective
usernoun1=input(str("Please enter a noun. ")) #asks for noun
userverbpasttense1=input(str("Please enter a verb in the past tense. ")) #asks for verb in past tense
useradverb1=input(str("Please enter an adverb. ")) #asks for adverb
useradj2=input(str("Please enter an adjective. ")) #asks for adjective
usernoun2=input(str("Please enter a noun. ")) #asks for noun
usernoun3=input(str("Please enter a noun. ")) #asks for noun
useradj3=input(str("Please enter an adjective. ")) #asks for adjective
userverb1=input(str("Please enter a verb. ")) #asks for verb
useradverb2=input(str("Please enter an adverb. ")) #asks for adverb
userverbpasttense2=input(str("Please enter a verb in the past tense. ")) #asks for verb in past tense
useradj4=input(str("Please enter an adjective. ")) #asks for adjective


madlibs=dict() #creates dictionary for madlibs
madlibs["adj1"]= useradj1 
madlibs["noun1"]=usernoun1
madlibs["verbpasttense1"]=userverbpasttense1
madlibs["adverb1"]=useradverb1
madlibs["adj2"]=useradj2
madlibs["noun2"]=usernoun2
madlibs["noun3"]=usernoun3
madlibs["adj3"]=useradj3
madlibs["verb1"]=userverb1
madlibs["adverb2"]=useradverb2
madlibs["verbpasttense2"]=userverbpasttense2
madlibs["adj4"]=useradj4
#programming logic
#below is main print statement
print("Today I went to the zoo. I saw a(n) "+str(madlibs["adj1"])+str(madlibs["noun1"])" jumping up and down in its tree. "+str(madlibs["verbpasttense1"])+str(madlibs["adverb1"]) "through the large tunnel that led to its "+str(madlibs["adj2"])+str(madlibs["noun2"])  "I got some peanuts and passed them through the cage to a gigantic gray "+str(madlibs["noun3"])" towering above my head. Feeding that animal made me hungry. I went to get a "+str(madlibs["adj3"]) "scoop of ice cream. It filled my stomach. Afterwards I had to "+str(madlibs["verb1"])+str(madlibs["adverb2"]) "to catch our bus. When I got home I "+str(madlibs["verbpasttense2"]) "my mom for a "+str(madlibs["adj4"]) "day at the zoo.")

每当我尝试它时,我都会在打印"verb1"的地方出现语法错误。我能做些什么来解决这个问题?此外,这并不重要,但我不知道如何将文本分开,而不是只放在一行。如果你也能帮上忙,那就太好了。

像这样的长行代码通常很难调试。你错过了那条线上的几个+标志。这是一个固定的代码行:

print("Today I went to the zoo. I saw a(n) "+str(madlibs["adj1"])+str(madlibs["noun1"])+" jumping up and down in its tree. "+str(madlibs["verbpasttense1"])+str(madlibs["adverb1"]) +"through the large tunnel that led to its "+str(madlibs["adj2"])+str(madlibs["noun2"])  + "I got some peanuts and passed them through the cage to a gigantic gray "+str(madlibs["noun3"]) + " towering above my head. Feeding that animal made me hungry. I went to get a "+str(madlibs["adj3"]) + "scoop of ice cream. It filled my stomach. Afterwards I had to "+str(madlibs["verb1"])+str(madlibs["adverb2"]) + "to catch our bus. When I got home I "+str(madlibs["verbpasttense2"]) + "my mom for a "+str(madlibs["adj4"]) + "day at the zoo.")

您还可以使用Python 3.6中的f-string:

print(f"Today I went to the zoo. I saw a(n) { str(madlibs['adj1']) } { str(madlibs['noun1']) } jumping up and down in its tree. {str(madlibs['verbpasttense1'])} {str(madlibs['adverb1'])} through the large tunnel that led to its {str(madlibs['adj2'])} {str(madlibs['noun2'])}. I got some peanuts and passed them through the cage to a gigantic gray {str(madlibs['noun3'])} towering above my head. Feeding that animal made me hungry. I went to get a(n) {str(madlibs['adj3'])} scoop of ice cream. It filled my stomach. Afterwards I had to {str(madlibs['verb1'])} {str(madlibs['adverb2'])} to catch our bus. When I got home I {str(madlibs['verbpasttense2'])} my mom for a {str(madlibs['adj4'])} day at the zoo.")

我强烈建议把它分成几行,也许每句话一行。

相关内容

最新更新