-2

I want to create classes in a loop. I've found this question: How do you create different variable names while in a loop?. But I want to associate a variable to a class, I can't use a list for that or a dictionary. How would I do so?

class Enemy():
    def __init__(self, type_):
        if type_ == 'demon':
            self.hp = random.randint(90, 110)
            self.dmg = [20, 40]
            self.dodge = 4
            loot_chance = random.randint(0, 10)
            if loot_chance <= 8:
                self.loot = loot(['hp', 'dmg'])
            if loot_chance == 9:
                self.loot = loot(['hp'])
            else:
                self.loot = loot(['dmg'])

I want to do this more efficiently:

enemy0 = Enemy('demon')
enemy1 = Enemy('demon')
enemy2 = Enemy('demon')
enemy3 = Enemy('demon')
enemy4 = Enemy('demon')
...
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
aaa
  • 93
  • 1
  • 7

1 Answers1

0

Rather than using individual variables you should use a list

You could then append your data into the list and access it using list[0] where 0 is the item you wish to retrieve.

Jacques
  • 927
  • 9
  • 18