mad libs上的Python语法错误
所以我试图制作一个简单的Mad Libs程序,但我遇到了多个错误,我不知道为什么。其中一个是“诺亚的第一个程序”部分,另一个是打印故事变量。我怎么修理它mad libs上的Python语法错误,python,syntax,Python,Syntax,所以我试图制作一个简单的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
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)
它似乎是一个Python 2.7程序 但是,如果您使用Python2.7,您将得到语法错误,因为在Python3中,
print
不是一个语句,而是一个函数:您需要括号
错:
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)
name = raw_input("Enter a name:")
如果您使用的是Python 3,请将raw\u input
替换为input
,将print…
替换为print(…)
。另外,根据,在分配变量时,在=
的两侧应该有一个空格,例如:
name=raw_input("Enter a name:")
应该是:
print STORY % (arg1, ..., argn)
name = raw_input("Enter a name:")
尽管不这样做不会导致语法错误。错误消息是什么?你安装了什么版本的Python?你可能需要避开撇号吗?我看到了卷曲的引号。你是用Word还是其他什么东西写的?我删除了撇号,正在运行2.7.11谢谢,这修复了这一部分,但在打印故事方面仍然有语法错误。
story
后缺少逗号:使用print(story,(形容词one,…)
。好的,非常感谢,这很有效,只是出现了一个新的错误。这就是它所展示的:诺亚的第一个程序!输入一个名称:Noah Traceback(最近一次调用):File“/Users/Noah/Desktop/Mad Libs 1.py”,第3行,在name=raw_input(“输入名称:”)中输入一个名称:eoferor:EOF读取一行时……嗯……您是如何运行脚本的?尝试打开命令行/终端并输入:python/Users/Noah/Desktop/Mad\Libs\1.py
。此外,脚本名称中最好不要有空格。Python命名此脚本的常用方法是madlibs.py
或mad_libs.py