mad libs上的Python语法错误

2024-04-18 15:51:40 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试着做一个简单的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)

Tags: andtonamefriendaninputrawis
2条回答

它似乎是一个python2.7程序。你知道吗

但是,如果使用Python2.7,就会出现语法错误,因为print在Python3中不是语句而是函数:需要括号。你知道吗

错误:

print "Noah's First Program!"

好的:

print("Noah's First Program!")

如果您使用的是Python 2,我相信这一点,那么您的代码应该如下所示:

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)

总结一下:

'替换撇号。你知道吗

修复字符串格式的语法:

print STORY(arg1, ..., argn)

应该是:

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

如果您使用的是python3,请用input替换raw_input,用print(...)替换print ...。另外,根据pep-8,在分配变量时,应该在=的两边有一个空格,例如:

name=raw_input("Enter a name:")

应该是:

name = raw_input("Enter a name:")

虽然不这样做不会导致语法错误。你知道吗

相关问题 更多 >