gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:45:40 25 4
gpt4 key购买 nike

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

已编辑:对这篇文章感到抱歉,它看起来写得很糟糕,所以我修正了。

最佳答案

这是一个范围变量问题。您需要输入全局变量,如下所示:

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

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

关于python - 为什么我的代码会出现此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16231479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com