我是一名開始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)。
編輯:對這篇文章感到抱歉,它似乎是相當可怕的書面,所以我修好了。
當錯誤發生時,請解開你的代碼的相關作品(+ - 5-7線)和包括它變成你的問題。 – akluth 2013-04-26 08:01:08
有一個'playerLeft'在_module_級別定義,但不在函數中 - 看起來像一個範圍問題?... – Basic 2013-04-26 08:19:09
@基本你可以寫出答案。我刪除了我的。不想拿你的信用。 – CppLearner 2013-04-26 08:22:43