0

I'm trying to make a simple RPG game. I have a list of people that will be in the game, and want to create a character for each of them.

people = ['Mike','Tom']
class Character(object):
    def __init__(self,name):
        self.name = name
        self.health = '100'

for x in people:
    [x] = Character(x) # This is where I don't know what to do / if it's possible

That way I can call a character easily like

for x in people:
    print ("%s: %s health" % (people[x].name, people[x].health))

Is such a thing possible, or am I going about this wrong? I read over How do I create a variable number of variables? but it seemed like dictionaries were the ultimate fix for their problem, and I have no idea how that would work in this situation.

Community
  • 1
  • 1
user230250
  • 131
  • 1
  • 10
  • 1
    Possible duplicate of [How do I do variable variables in Python?](http://stackoverflow.com/questions/1373164/how-do-i-do-variable-variables-in-python) – jonrsharpe Apr 14 '16 at 17:30

1 Answers1

1

It looks like you're just wanting to keep a list of Character objects. You don't need to reference people once you have that.

characters = [Character(person) for person in people]

Then, to print the character stats:

for c in characters:
    print ("%s: %s health" % (c.name, c.health))
Darrick Herwehe
  • 3,553
  • 1
  • 21
  • 30