2016-02-18 75 views
1

我正在學習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新手,所以請幫忙解釋一下。 在此先感謝

+1

您的縮進在多處出現錯誤;請修復它以匹配您實際擁有的內容。 「fight_answer_y」的使用看起來是正確的,但如果縮進甚至有點偏離,它可能很容易破壞你描述的方式。 – ShadowRanger

+0

下面是我的代碼更好看:http://paste.ofcode。org/Lby8kqUghyhmKyms2QMP9C –

回答

0

簡短的回答:始終配置編輯器選項卡要擴大到四個空格,每PEP8,不要只把它顯示選項卡爲四個空格,因爲它隱藏了混合製表符和空格的問題。

解釋:在your paste,問題是,一個elif和相關聯的塊的縮進太淺,附着於外if代替內if它應該附加到。問題是因爲你在混合空格和製表符,if fight_answer_y == "blame them":是縮進兩個製表符,而elif fight_answer_y == "apologize":是縮進的八個空格。你可能使用了一個將製表符顯示爲四個空格的編輯器(Stack Overflow也這樣做),所以它們出現在相同的對齊位置,但Python 2總是將一個製表符視爲相當於八個空格,所以代碼如下所示:

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": # <-- ***LOOKS LIKE ATTACHED TO if fight_answer_y == "blame them":*** 
     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. 

感謝您的編輯器顯示選項卡四個空間縮進被解釋爲:

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": # <-- ***ACTUALLY ATTACHED TO if fight_answer == "yes":*** 
    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. 

感謝Python的治療相同的選項卡八個空格縮進。你倒黴了,縮進似乎是有效的(大多數其他失敗像這意味着SyntaxErrors,或導致代碼退出方法定義,所以失敗是顯而易見的)。在這種情況下,這意味着當fight_answer不是"yes"時,您嘗試測試fight_answer_y == "apologize",但fight_answer_y僅在fight_answer == "yes"的情況下定義,意思是fight_answer_y未定義。

如果您將您的編輯器配置爲遵循PEP8(將選項卡擴展到空格,使用四個空格縮進),對齊問題將是顯而易見的。如果你在Python 3上運行,那麼它不太容忍不一致地使用製表符和空格,並且你的代碼會報告TabError: inconsistent use of tabs and spaces in indentation的問題。但事實是,你的代碼看起來是正確的,並且出錯了。再次:總是使用PEP8規則的空白,以避免這樣的問題。每個編輯器(除了記事本)都可以配置爲將選項卡擴展爲具有四個空格縮進的空格;做到這一點。

0

您的縮進是錯誤的。據我所知,這似乎工作。

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() 
+0

謝謝!你能告訴我確切的部分,我錯了,因爲我花了幾個小時看代碼,我沒有遇到任何縮進錯誤。以下是我的代碼更好看:http://paste.ofcode.org/Lby8kqUghyhmKyms2QMP9C –

相關問題