Каждый существующий объект в этом списке должен быть заменен экземпляром определенного класса.Каждый существующий объект уже имеет тип .type, который должен соединяться с ключом в словаре, чтобы создать соответствующий класс (значение в dict).Затем объектам также необходимо восстановить свое исходное имя. Перед назначением класса.
Я не могу найти способ сделать это, не включающий вложенные циклы for, но вложенные циклы for в основном присваиваются КАЖДОМУчлен класса с тем же именем (фамилия сохранена во временной переменной).
Я перерыл всю основную литературу и понимаю основную логику, связанную с циклами, словарями и списками.просто (уверен, что я) выполняю что-то неправильно или вкладываю что-то неправильно.
class gameObject(object):
def __init__(self):
self.name = name
#Class given to any living thing in the game; confers basic stats
class livingThing(gameObject):
def __init__(self, name="Living Thing", HP = 0):
self.name = name
self.HP = HP
self.alive = True
self.safe = True
self.listready = False
# After livingThing, classes narrow into more specific groups that have unique traits, abilities, and roles in the game
class Animal(livingThing):
def __init__(self):
super().__init__()
self.truename = ""
self.type = ""
self.listready = False
self.type = "Test"
self.truetype = ""
self.hasatype = False
class Reptile(Animal):
def __init__(self):
super().__init__()
self.therm = "ecto"
self.type = "Game Reptile"
class Amphibian(Animal):
def __init__(self):
super().__init__()
self.therm = "ecto"
self.type = "Game Amphibian"
class Bird(Animal):
def __init__(self):
super().__init__()
self.therm = "endo"
self.type = "Game Bird"
class Mammal(Animal):
def __init__(self):
super().__init__()
self.therm = "endo"
self.type = "Game Mammal"
class Fungus(Animal):
def __init__(self):
super().__init__()
self.therm = "none"
self.type = "Game Fungus"
class Fungus(Animal):
def __init__(self):
super().__init__()
self.therm = "none"
self.type = "Game Fungus"
class Ascomycetes(Animal):
def __init__(self):
super().__init__()
self.therm = "none"
self.type = "Game Ascomycetes"
somereptile = Reptile()
somereptile.type = "Reptiles"
somereptile.name = "Some Reptile"
somefrog = Amphibian()
somefrog.type = "Amphibians"
somefrog.name = "Some frog"
somefungus = Fungus()
somefungus.type = "Fungi"
somefungus.name = "Some Fungus"
secondfrog = Amphibian()
secondfrog.type = "Amphibians"
secondfrog.name = "Second Frog"
thirdfrog = Amphibian()
thirdfrog.type = "Amphibians"
thirdfrog.name = "Third Frog"
secondfungus = Fungus()
secondfungus.type = "Fungi"
secondfungus.name = "Second Fungus"
dummypop = [somereptile, somefrog, somefungus, secondfrog, thirdfrog, secondfungus]
### PROBLEM FUNCTION ###
def givetype(poplist):
typedict = {
"Reptiles" : Reptile(),
"Amphibians" : Amphibian(),
"Birds" : Bird(),
"Mammals" : Mammal(),
"Fungi" : Fungus(),
"Ascomycetes" : Ascomycetes()
}
holderlist = []
tempnames = []
i = 0
for org in poplist:
holderlist.append(org)
tempnames.append(org.name)
for key in typedict.keys():
if (holderlist[i].type.lower() in key.lower()):
holderlist[i] = typedict[key]
holderlist[i].name = tempnames[i]
print(holderlist[i].name,
holderlist[i].type)
i+=1
return holderlist
dummymaster = (givetype(dummypop))
for animal in dummymaster:
print(animal.name, animal.type)
Я рассчитываю произвести:
Some Reptile Game Reptile
Some Frog Game Frog
Third Frog Game Frog
Second Frog Game Frog
etc
Что я получаю:
Some Reptile Game Reptile
Third Frog Game Frog
Third Frog Game Frog
Third Frog Game Frog
etc
Спасибо за вашу помощь!