python - 我需要帮助从类函数的列表中删除条目
问题描述
在我正在编写的一个程序中,每当玩家攻击敌人时,都会运行一个 if 语句来检查目标的健康状况:
def attack(self, target):
target.stats.health = target.stats.health - self.stats.damage
if target.stats.health <= 0:
if isinstance(target, Enemy) == True:
print(target.name, "has died")
del #Something here to delete the target from the list of enemies
#For example, in myPlayer.attack(enemies[0])
#The target is enemies[0]
else:
pass
else:
print(target.name, "has", target.stats.health, "health left")
在我的代码中,创建了 Enemy 类的实例来创建敌人,这些实例都被放入一个列表中。如果目标的健康状况低于,我想要做的是删除列表条目。我知道目标本身是对我的敌人实例的引用,所以使用del target
只会删除该引用。有没有一种方法可以让我实际调用参数条目或其他东西并将其删除?因此,如果我输入myPlayer.attack(enemies[x])
,该del
函数将作为del enemies[x]
.
解决方案
推荐阅读
- miller - 是否有米勒函数可以将格式化为货币的 csv 数字识别为数字而不是字符串?
- python - 由于 OSError:libopencv_gapi.so.4.1,无法在 anaconda 环境中运行 python 应用程序
- report - 如何检查 Google“AutoML”表的结果?
- reactjs - React - 对象数组 - 根据输入更新对象属性
- python - 将循环结果存储在嵌套列表中
- foldable-devices - Surface Duo React Native 因“Stub!”而崩溃 错误
- python - python从大型mxm网格中提取每个可能的nxn网格
- asp.net - 缓存反应路由器后无法通过浏览器访问asp控制器路由
- c# - 当泛型类型是类型的父类型时,无法将类型隐式转换为泛型类型
- asp.net-core - 在 Visual Studio Mac 2019 中调试 .Net Core 3.1 源代码