2013-04-26 85 views
1

我是一名開始Python程序員,剛剛完成我的第一個程序Battle。 但我有問題。至於我可以看到,所有的代碼工作,除了接近尾聲的一部分罰款,在那裏我得到一個UnboundLocalError,一起來看看:爲什麼我的代碼會出現此錯誤?

Traceback (most recent call last): 
File "[Dir]\Programs\Battle.py", line 210, in <module> 
callUserCombat() 
File "[Dir]\Programs\Battle.py", line 26, in callUserCombat 
playerLeft = str(playerLeft) 
UnboundLocalError: local variable 'playerLeft' referenced before assignment 

而這正是它引用(不小的金額,對不起):

#Define callUserCombat(). 
def callUserCombat(): 
    print('Your turn.') 
    time.sleep(2) 
    print() 
    print('Choose a move:') 
    print('Attack 1 [2 - 3 Damage] (1)') 
    print('Attack 2 [0 - 5 Damage] (2)') 
    print('Heal [Heals 0 - 6] (3)') 
    playerLeft = str(playerLeft) 
    enemyLeft = str(enemyLeft) 
    print('Your HP: ' + playerLeft) 
    print('Enemy HP: ' + enemyLeft) 
    chosenAttack = input() 

    while chosenAttack != '0': 
     if chosenAttack == '1': 
      chosenAttackPower = random.randint(2,3) 
      enemyLeft = - chosenAttackPower 
      chosenAttackPower = str(chosenAttackPower) 
      print('You attacked!') 
      print('Enemy lost ' + chosenAttackPower + 'HP. ') 
      break 

     if chosenAttack == '2': 
      chosenAttackPower = random.randint(0,5) 
      enemyLeft = - chosenAttackPower 
      chosenAttackPower = str(ChosenAttackPower) 
      print('You attacked!') 
      print("Enemy lost " + chosenAttackPower + 'HP. ') 
      break 

     if chosenAttack == '3': 
      chosenAttackPower = random.randint(1,5) 
      playerLeft = + chosenAttackPower 
      chosenAttackPower = str(ChosenAttackPower) 
      print('You healed!') 
      print('Healed ' + chosenAttackPower + 'HP.') 
      break 

     else: 
      chosenAttack = '0' 
      print('Please enter a valid move code.') 
      print("Attack 1 (1)") 
      print("Attack 2 (2)") 
      print("Heal (3)") 
      print() 
      chosenAttack = input() 
     limitHealth() 

#Define callEnemyCombat(). 
def callEnemyCombat(): 
    print('Enemy\'s turn.') 
    time.sleep(2) 
    print() 
    enemyAttack = random.randint(1,6) 
    if enemyAttack == '1': 
     enemyAttackPower = random.randint(2,3) 
     playerLeft = - enemyAttackPower 
     enemyAttackPower = str(enemyAttackPower) 
     print('Don\'t bother taunting me, you\'ll die trying. And it\'s mean.') 
     print('Enemy attacked!') 
     print('You lost ' + enemyAttackPower + 'HP. ') 

    if enemyAttack == '2': 
     enemyAttackPower = random.randint(2,3) 
     playerLeft = - enemyAttackPower 
     enemyAttackPower = str(enemyAttackPower) 
     print('Where do you live? I only want to know so I can kill') 
     print('your family once I\'m finished with you.') 
     print('Enemy attacked!') 
     print('You lost ' + enemyAttackPower + 'HP. ') 

    if enemyAttack == '3': 
     enemyAttackPower = random.randint(2,3) 
     playerLeft = - enemyAttackPower 
     enemyAttackPower = str(enemyAttackPower) 
     print('I can take anything you can throw at me. Except Chocolate.') 
     print('I\'m allergic to Chocolate.') 
     print('Enemy attacked!') 
     print('You lost ' + enemyAttackPower + 'HP. ') 

    if enemyAttack == '4': 
     enemyAttackPower = random.randint(0,5) 
     playerLeft = - enemyAttackPower 
     enemyAttackPower = str(enemyAttackPower) 
     print('I WILL kill you, even if I have to hire the Men in Black to do it.') 
     print('Enemy attacked!') 
     print('You lost ' + enemyAttackPower + 'HP. ') 

    if enemyAttack == '5': 
     enemyAttackPower = random.randint(0,5) 
     playerLeft = - enemyAttackPower 
     enemyAttackPower = str(enemyAttackPower) 
     print('Have you heard of pong? We could play pong after I kill you,') 
     print('if you like.') 
     print('Enemy attacked!') 
     print('You lost ' + enemyAttackPower + 'HP. ') 

    if enemyAttack == '6': 
     enemyAttackPower = random.randint(1,5) 
     enemyLeft = + enemyAttackPower 
     enemyAttackPower = str(ChosenAttackPower) 
     print('Charging up with a cold glass of water, and a side of you.') 
     print('Enemy healed!') 
     print('Healed ' + chosenAttackPower + 'HP.') 
    limitHealth() 

#Define limitHealth(). 
def limitHealth(): 
    if playerLeft > playerHealth: 
     playerLeft = playerHealth 

    if enemyLeft > enemyHealth: 
     enemyLeft = enemyHealth 

這裏是代碼,因此,所有有意義: (https://www.dropbox.com/s/9l4ifgmk5sx1pg1/Battle.py)。

編輯:對這篇文章感到抱歉,它似乎是相當可怕的書面,所以我修好了。

+3

當錯誤發生時,請解開你的代碼的相關作品(+ - 5-7線)和包括它變成你的問題。 – akluth 2013-04-26 08:01:08

+0

有一個'playerLeft'在_module_級別定義,但不在函數中 - 看起來像一個範圍問題?... – Basic 2013-04-26 08:19:09

+0

@基本你可以寫出答案。我刪除了我的。不想拿你的信用。 – CppLearner 2013-04-26 08:22:43

回答

2

這是一個範圍可變問題。你需要爲這樣的輸入您的全局變量:

enemyHealth = 1 
playerLeft = 2 
enemyLeft = 2 
... 

#Define callUserCombat(). 
def callUserCombat(): 
    global enemyHealth 
    global playerLeft 
    global enemyLeft 
    etc. 
+7

答案可以解決問題,但不使用'global'就可以重寫代碼,這將是更好的選擇。 – Matthias 2013-04-26 08:31:07

+0

我不認爲這是一個範圍問題。因爲我已經運行了一些代碼而不使用'global'。 http://pastie.org/7723221#2。我使用'SPROUT ='@''而不使用'全局' ' – Moj 2013-04-26 08:39:12

+0

@Matthias:我剛剛回答了OP問題「爲什麼我的代碼會出現此錯誤?」。但我同意你的看法:可變全局變量是危險的。 – lucasg 2013-04-26 08:49:52

0

絕對是有範圍的問題,更具體地說,在蟒蛇試圖解決分配給它(時名的方式,即當你試圖給變量分配playerLeft)。你可以看這裏:http://eli.thegreenplace.net/2011/05/15/understanding-unboundlocalerror-in-python/瞭解完整的說明。

由於您似乎只是試圖打印全局變量的值,所以我們需要輸入playerLeft。只要做到這一點,正是:

playerLeft = 2 
enemyLeft = 2 
... 

#Define callUserCombat(). 
def callUserCombat(): 

    ... 
    print('Your HP: ', playerLeft) 
    print('Enemy HP: ', enemyLeft) 

    ... 

應避免問題

相關問題