0

Forgive me if the title is a bit incorrect, I'm very new to Python's Classes and don't exactly know what the names of it's various intricacies. Best guess!

Hello SO! I'm working on a little example project and have run into a roadblock. Currently I'm attempting to create a basic combat tracker of sorts for a tabletop game. My current goal is to create a system where, based off of user inputs, one can raise or lower the HP for a preset object belonging to the class 'Entity' by simply typing in the name of the specific entity and the corresponding number.

For example, if orkBoy's Wounds (aka HP) were set to 12 by default and you wanted to increase it to 15, you would type in:

orkBoy 3

Or if you wished to decrease it by 2:

orkBoy -2

While the system works perfectly if you directly reference the object in the code, I am having trouble referencing the object through the user's input string due to a str Attribute Error (AttributeError: 'str' object has no attribute 'woundChange'). I will place the code below, including a try/except that Tries and fails at what I want to accomplish, and an Exception that provides an unsatisfactory but successful 'solution'.

class Entity:

    woundChange = 0

    def __init__(self, name, wounds, movement, weapon, melee):
        self.name = name
        self.wounds = wounds
        self.movement = movement
        self.weapon = weapon
        self.melee = melee

    def array(self):
        return '{}; HP: {}, {}, {}, {}'.format(self.name, self.wounds, self.movement, self.weapon, self.melee)

    def apply_damage(self):
        self.wounds = int(self.wounds + self.woundChange)



imperialGuard = Entity('Imperial Guardsman', 10, '3/6/9/18', 'Lasgun', 'Knife')
orkBoy = Entity('Ork Boy', 12, '3/6/9/18', 'Slugga', 'Choppa')


entity_damage = input('Input entity & wound change.\n')
entity_damage = entity_damage.split(' ')
print(entity_damage)
print('')

entityName = entity_damage[0]
entityNum = int(entity_damage[1])

try:
    entityName.woundChange = int(entityNum)
    entityName.apply_damage()
except AttributeError:
    print('No dice.')
    orkBoy.woundChange = int(entityNum)
    print(orkBoy.wounds)
    orkBoy.apply_damage()
    print(orkBoy.wounds)

Thank you for reading, and I wish you a happy Tuesday!

1
  • Store you instances in a single dict, not individual variables. Commented May 11, 2021 at 21:21

1 Answer 1

1

Use a dict:

entities = {
    'imperialGuard': Entity('Imperial Guardsman', 10, '3/6/9/18', 'Lasgun', 'Knife')
    'orkBoy': Entity('Ork Boy', 12, '3/6/9/18', 'Slugga', 'Choppa')
}

Then you can use the user input to look up the appropriate object. For example,

entities[entityName].apply_damage()
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect solution, thank you kindly chepner!

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.