为什么我的代码会出现此错误?

时间:2013-04-26 08:00:04

标签: python python-3.x

我是一名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)。

编辑:对这篇文章感到抱歉,它写得非常糟糕,所以我修复了。

2 个答案:

答案 0 :(得分:2)

这是一个范围变量问题。您需要输入全局变量:

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

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

答案 1 :(得分:0)

绝对是范围问题,更具体地说是python在尝试分配名称时解析名称的方式(即当您尝试分配变量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) 

    ...

应该避免这个问题