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)。

编辑:对不起,这篇文章似乎写得很糟糕,所以我修好了。

4

2 回答 2

2

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

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

#Define callUserCombat().
def callUserCombat():
     global enemyHealth
     global playerLeft 
     global enemyLeft 
     etc.
于 2013-04-26T08:26:19.390 回答
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) 

    ...

应该避免问题

于 2013-04-26T08:44:30.320 回答