不能 select 反对列表中的对象并将其删除
Can't select object in list and delete it
我现在总共花了一两个小时试图解决这个问题,但我没有得到任何结果。在我的程序中,当防御动物 "killed" 时,它应该从它的注册表中删除,但无论我做什么,我似乎都无法做到这一点。在这种情况下,如果我要启动猎人和水牛,然后攻击()水牛和猎人,水牛应该被杀死并从它的 类 注册表中删除,但我无法 python 到 select 注册表中的特定水牛。
感谢任何帮助。
class IterRegistry(type):
def __iter__(cls):
return iter(cls._registry)
class Buffalo(object):
__metaclass__ = IterRegistry
_registry = []
hp = 1
price = 150
attacks = 0
regeneration = 2
def __init__(self, name):
self._registry.append(self)
self.name = name
def createBuffalo():
for i in range(len(Buffalo._registry),len(Buffalo._registry)+1):
varname = ("b" + str(i))
globals()[varname] = Buffalo("b" + str(i))
class Wolf:
__metaclass__ = IterRegistry
_registry = []
hp = 1
price = 0
attacks = 2
regeneration = 1.5
def __init__(self, name):
self._registry.append(self)
self.name = name
def createWolf():
for i in range(len(Wolf._registry),len(Wolf._registry)+1):
varname = ("w" + str(i))
globals()[varname] = Wolf("w" + str(i))
class Hunter:
__metaclass__ = IterRegistry
_registry = []
hp = 2
price = 0
attacks = 1
regeneration = 0
balance = 0
def __init__(self, name):
self._registry.append(self)
self.name = name
def createHunter():
for i in range(len(Hunter._registry),len(Hunter._registry)+1):
varname = ("h" + str(i))
globals()[varname] = Hunter("h" + str(i))
def attack(attacker, target):
if attacker.attacks >= 1:
target.hp -= 1
if target.hp == 0:
if type(attacker) == Hunter:
attacker.balance += 150
print(target)
if type(target) == Wolf:
del Wolf._registry[[n for n in Wolf._registry if n == target]]
if type(target) == Hunter:
del Hunter._registry[[n for n in Hunter._registry if n == target]]
if type(target) == Buffalo:
del Buffalo._registry[[n for n in Hunter._registry if n == target]]
另外请注意,我使用双括号的原因是单括号的语法不正确,但双括号的语法没问题。
如果您需要更多代码,请在下面的评论中提问(我没有包含所有代码,但应该足够了)。
您尝试解决的问题是由于尝试使用列表作为要从列表中删除的索引引起的
[n for n in Wolf._registry if n == target]
你想要摆脱这个列表理解的只是目标,所以就用它吧。
Wolf._registry.remove(target)
我现在总共花了一两个小时试图解决这个问题,但我没有得到任何结果。在我的程序中,当防御动物 "killed" 时,它应该从它的注册表中删除,但无论我做什么,我似乎都无法做到这一点。在这种情况下,如果我要启动猎人和水牛,然后攻击()水牛和猎人,水牛应该被杀死并从它的 类 注册表中删除,但我无法 python 到 select 注册表中的特定水牛。
感谢任何帮助。
class IterRegistry(type):
def __iter__(cls):
return iter(cls._registry)
class Buffalo(object):
__metaclass__ = IterRegistry
_registry = []
hp = 1
price = 150
attacks = 0
regeneration = 2
def __init__(self, name):
self._registry.append(self)
self.name = name
def createBuffalo():
for i in range(len(Buffalo._registry),len(Buffalo._registry)+1):
varname = ("b" + str(i))
globals()[varname] = Buffalo("b" + str(i))
class Wolf:
__metaclass__ = IterRegistry
_registry = []
hp = 1
price = 0
attacks = 2
regeneration = 1.5
def __init__(self, name):
self._registry.append(self)
self.name = name
def createWolf():
for i in range(len(Wolf._registry),len(Wolf._registry)+1):
varname = ("w" + str(i))
globals()[varname] = Wolf("w" + str(i))
class Hunter:
__metaclass__ = IterRegistry
_registry = []
hp = 2
price = 0
attacks = 1
regeneration = 0
balance = 0
def __init__(self, name):
self._registry.append(self)
self.name = name
def createHunter():
for i in range(len(Hunter._registry),len(Hunter._registry)+1):
varname = ("h" + str(i))
globals()[varname] = Hunter("h" + str(i))
def attack(attacker, target):
if attacker.attacks >= 1:
target.hp -= 1
if target.hp == 0:
if type(attacker) == Hunter:
attacker.balance += 150
print(target)
if type(target) == Wolf:
del Wolf._registry[[n for n in Wolf._registry if n == target]]
if type(target) == Hunter:
del Hunter._registry[[n for n in Hunter._registry if n == target]]
if type(target) == Buffalo:
del Buffalo._registry[[n for n in Hunter._registry if n == target]]
另外请注意,我使用双括号的原因是单括号的语法不正确,但双括号的语法没问题。
如果您需要更多代码,请在下面的评论中提问(我没有包含所有代码,但应该足够了)。
您尝试解决的问题是由于尝试使用列表作为要从列表中删除的索引引起的
[n for n in Wolf._registry if n == target]
你想要摆脱这个列表理解的只是目标,所以就用它吧。
Wolf._registry.remove(target)