我正在學習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_answer_y」 這裏會得到一個錯誤,我發現了錯誤:
File "mygame1.py", line 38, in fight
elif fight_answer_y == "apologize":
UnboundLocalError: local variable 'fight_answer_y' referenced before assignment
任何人都可以因爲我是Python新手,所以請幫忙解釋一下。 在此先感謝
您的縮進在多處出現錯誤;請修復它以匹配您實際擁有的內容。 「fight_answer_y」的使用看起來是正確的,但如果縮進甚至有點偏離,它可能很容易破壞你描述的方式。 – ShadowRanger
下面是我的代碼更好看:http://paste.ofcode。org/Lby8kqUghyhmKyms2QMP9C –