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.

1

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.