疯狂libs

时间:2016-08-15 21:04:36

标签: python syntax

所以我试图制作一个简单的Mad Libs程序,我遇到了多个错误,我无法弄清楚原因。其中一个是“Noah的第一个程序”部分,另一个是故事变量的打印。我该如何解决?

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)

2 个答案:

答案 0 :(得分:1)

如果您使用的是Python {2 raw_input让我相信的话,那么您的代码应如下所示:

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)

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

name=raw_input("Enter a name:")

应该是:

name = raw_input("Enter a name:")

虽然不这样做但不会导致语法错误。

答案 1 :(得分:-1)

它似乎是一个Python 2.7程序。

但是,如果你使用Python 2.7,你会得到语法错误,因为print不是一个语句,而是Python 3中的一个函数:你需要括号。

错:

print "Noah's First Program!"

好:

print("Noah's First Program!")