-1

I'm trying to make a game with enemies that spawn consistently. I've done something like this in the past but used the exec("enemy%s = Enemy()" % x) command to consistently create instances of a class. I was wondering if there are any more effective ways to do this. Please link me to a duplicate if there are any as I couldn't find any.

I've tried

virus = {virus0 : Virus()}
for x in range(1, 11):
    virus.append((virus : Virus()) % x)

but this returned invalid syntax. I don't know how to correctly do this with a dictionary nor list.

AlphaZetta
  • 16
  • 4

1 Answers1

1

You don't need x at all; the list indices are the labels.

viruses = [Virus() for _ in range(10)]

or in longer form

viruses = []
for _ in range(10):
    viruses.append(Virus())

Now instead of virus0, virus1, etc, you use viruses[0], viruses[1], etc.

chepner
  • 497,756
  • 71
  • 530
  • 681