如何解决此错误?
Traceback (most recent call last):
You attacked Magus for 291 points of damage.
File "/Users/bill/Documents/Dev/PycharmProjects/Battle/main.py", line 189, in <module>
spell, magic_dmg = enemy.choose_enemy_spell()
它有时只会发生。我不知道为什么它有时会发生,为什么有时只有这些数据是&#34; NoneType&#34;。
for enemy in enemies:
enemy_choice = random.randrange(0, 2)
if enemy_choice == 0:
target = random.randrange(0, 2)
enemy_dmg = enemies[0].generate_damage()
players[target].take_damage(enemy_dmg)
print(enemy.name.replace(" ", "") + " attacks " + players[target].name.replace(" ", "") + " for", str(enemy_dmg) + ".")
elif enemy_choice == 1:
spell, magic_dmg = enemy.choose_enemy_spell()
enemy.reduce_mp(spell.cost)
if spell.type == "white":
enemy.heal(magic_dmg)
print(bcolors.OKBLUE + spell.name + " heals " + enemy.name.replace(" ", "") + " for", str(magic_dmg), "HP." + bcolors.ENDC)
elif spell.type == "black":
target = random.randrange(0, 2)
players[target].take_damage(magic_dmg)
print(bcolors.OKBLUE + "\n" + enemy.name.replace(" ", "") + "'s " + spell.name + " deals", str(magic_dmg),
"points of damage to " + players[target].name.replace(" ", "") + bcolors.ENDC)
if players[target].get_hp() == 0:
print(players[target].name.replace(" ", "") + " has died.")
del players[player]
以下是完整的代码:https://pastebin.com/2tztP4P4
答案 0 :(得分:1)
错误在第400行。您没有在那里返回任何内容,这意味着您将返回None
,但无法将其解包为spell, magic_dmg
。
答案 1 :(得分:0)
您需要更改choose_enemy_spell函数,以便在通过递归调用它时返回
self.choose_enemy_spell()
更改为
return self.choose_enemy_spell()
更新的功能在
之下def choose_enemy_spell(self):
magic_choice = random.randrange(0, len(self.magic))
spell = self.magic[magic_choice]
magic_dmg = spell.generate_damage()
pct = self.hp / self.maxhp * 100
if self.mp < spell.cost or spell.type == "white" and pct > 50:
return self.choose_enemy_spell()
else:
return spell, magic_dmg