我正在学习python的艰苦的道路课程,我不得不使我自己的小故事使用python。我确实做到了,我花了很多时间来解决它的许多问题,但有一个变量导致了我的许多错误。你知道吗
这是我的密码:
def party():
print "You are at a friend's house, and there you find three men who disrespect you.\
\nDo you fight them, insult back, or do you invite them for a drink?"
party_answer = raw_input()
while (party_answer != "fight") and (party_answer != "insult") and (party_answer != "drink"):
print "Type either 'fight', 'insult', or 'drink'"
party_answer = raw_input()
if party_answer == "fight":
fight()
elif party_answer == "insult":
insult()
elif party_answer == "drink":
drink()
def fight():
print """You decide to fight the men.
After a while, the men start pleading for mercy.
Do you hit them more?"""
fight_answer = raw_input()
while (fight_answer != "yes") and (fight_answer != "no"):
print "Type either 'yes' or 'no'"
fight_answer = raw_input()
if fight_answer == "yes":
print """Your friend sees you fighting and asks you why
Do you blame them, or do you take responsibility and apologize?"""
fight_answer_y = raw_input()
while (fight_answer_y != "blame them") and (fight_answer_y != "apologize"):
print "Type either 'blame them' or 'apologize'"
fight_answer_y = raw_input()
if fight_answer_y == "blame them":
print """Your friend is angry at you fighting at all, and does not believe \
that they started the fight. He kicks you out of the party."""
exit(1)
elif fight_answer_y == "apologize":
print "Your friend forgives you, and asks you to drink with him."
exit(2)
elif fight_answer == "no":
print """The men take advantage of you forgiveness and start hitting you.
Fortunately, your friend arrives at this moment. He see them hitting you, \
and then kicks them out of the party."""
exit(2)
def insult():
print """You insult them, and they challenge you to a fight.
Do you fight them or tell your friend?"""
insult_answer = raw_input()
while (insult_answer != "fight") and (insult_answer != "tell friend"):
print "Type either 'fight' or 'tell friend'"
insult_answer = raw_input()
if insult_answer == "fight":
fight()
elif insult_answer == "tell friend":
print """You decide to tell your friend.
He says he is happy you told him, and invites you for a drink
The men get kicked out"""
exit(2)
def drink():
print """You tell them that you will buy drinks for them,\
and they rudely ask you why you are doing this.
Do you insult them or give them a polite response?"""
drink_answer = raw_input()
while (drink_answer != "insult") and (drink_answer != "polite response"):
print "Type either 'insult' or 'polite response'"
drink_answer = raw_input()
if drink_answer == "insult":
insult()
elif drink_answer == "polite response":
print "You politely respond to them, and they sit down with you and apologize."
exit(2)
def exit(x):
if x == 1:
print "You lost. Better luck next time!"
again = raw_input("Do you want to try again?: ")
while (again != "yes") and (again != "no"):
print "Type either 'yes' or 'no'"
again = raw_input("Do you want to try again?: ")
if again == "yes":
party()
elif again == "no":
quit()
elif x == 2:
print "You won. Good job!"
again = raw_input("Do you want to try again?: ")
while (again != "yes") and (again != "no"):
print "Type either 'yes' or 'no'"
again = raw_input("Do you want to try again?: ")
if again == "yes":
party()
elif again == "no":
quit()
party()
我好像在变量“fight\u answer\y”上出错了 下面是我得到的错误:
File "mygame1.py", line 38, in fight
elif fight_answer_y == "apologize":
UnboundLocalError: local variable 'fight_answer_y' referenced before assignment
有谁能帮我解释一下,因为我是Python新手。 提前谢谢
简短回答:总是将编辑器配置为将选项卡扩展到四个空格,不要让它将选项卡显示为四个空格,因为它隐藏了混合选项卡和空格的问题。你知道吗
说明:在your paste中,问题是
elif
和相关的块缩进太浅,连接到外部的if
,而不是应该连接到的内部的if
。问题是因为你混合了空格和制表符,if fight_answer_y == "blame them":
用两个制表符缩进,而elif fight_answer_y == "apologize":
用八个空格缩进。您可能使用的编辑器将选项卡显示为四个空格(堆栈溢出也会这样做),因此它们以相同的对齐方式出现,但Python 2始终将选项卡视为等效于八个空格,因此代码如下所示:由于编辑器将选项卡显示为四个空格缩进,因此将其解释为:
感谢Python将相同的制表符视为8个空格缩进。不幸的是,缩进看起来是有效的(像这样的大多数其他失败意味着语法错误,或者导致代码从方法定义中退出,所以失败是显而易见的)。在这种情况下,这意味着当
fight_answer
不是"yes"
时,您尝试测试fight_answer_y == "apologize"
,除了fight_answer_y
仅在fight_answer == "yes"
情况下定义,这意味着fight_answer_y
未定义。你知道吗如果将编辑器配置为遵循PEP8(将制表符扩展到空格,使用四个空格缩进),对齐问题将是显而易见的。如果您是在python3上运行的,那么它就不能容忍不一致地使用制表符和空格,并且您的代码会报告
TabError: inconsistent use of tabs and spaces in indentation
的问题。但实际上,您的代码看起来是正确的,但运行错误。同样:总是对空格使用PEP8规则来避免这样的问题。每个编辑器(除了记事本之外)都可以配置为将制表符扩展到具有四个空格缩进的空格;做到这一点。你知道吗你的压痕不对。据我所知,这似乎奏效了。你知道吗
相关问题 更多 >
编程相关推荐