0

I'm not even sure you can do this. I am trying to get the integer values from attack_moves: fighter: sword punch kick in the hope that I can use these values later in an attack on a monster.

from random import randint

class Human(object):
    # Question is
    # Is there a way of getting sword, punch and kick interger values?
    attack_moves = {
        'fighter': {'sword': randint(1,8),'punch': randint(1,4),
        'kick': randint(1,5)},
        'Wizard': {'fire ball': randint (1,7),'staff': randint(1,6)},
        }

    def __init__(self, name,
                 present_human_hit_points, max_human_hit_points):

        self.name = name
        self.present_human_hit_points = present_human_hit_points
        self.max_human_hit_points = max_human_hit_points

    def decrease_health(self, amount):
        self.amount = amount
        death = False
        while not death:
            if self.present_human_hit_points > 0:
                self.present_human_hit_points -= amount
                print self.present_human_hit_points
            else:
                death = True


class Fighter(Human):

    def __init__(self, name,
                 present_human_hit_points, max_human_hit_points):

        Human.__init__(self, name,
                       present_human_hit_points, max_human_hit_points)
        Human.attack_moves = Human.attack_moves.get('fighter')

    #def attack_type(self, attack_moves):
#        self.attack_moves = Human.attack_moves
#        print attack_moves


human = Fighter("bob", 50, 100)
print human.name, human.present_human_hit_points,
print human.max_human_hit_points
print human.attack_moves
tcratius
  • 525
  • 6
  • 15
  • 1
    This is straightforward dictionary access - what did you try? – cco Sep 22 '17 at 05:03
  • You're overwriting the dictionary when you do `Human.attack_moves = Human.attack_moves.get('fighter')`. Did you really mean to assign to the same variable? – Barmar Sep 22 '17 at 05:04
  • You probably should be doing `self.attack_moves = ...` – Barmar Sep 22 '17 at 05:04
  • Or maybe `Fighter.attack_moves = ...` – Barmar Sep 22 '17 at 05:05
  • To access nested dictionaries, just add additional indexing: `Human.attack_moves['fighter']['sword']` – Barmar Sep 22 '17 at 05:05
  • You are overwriting the *class level* (i.e. *static*) attribute that contains your dictionary here: `Human.attack_moves = Human.attack_moves.get('fighter')`. You probably don't want to do that. the `__init__` generally serves the purpose of *initializing instance variables*, so maybe something like `self.attack_moves = Human.attack_moves.get('fighter')`? – juanpa.arrivillaga Sep 22 '17 at 05:06
  • Sorry everyone, I'm still learning. Accessing using nested dictionaries; couldn't seem to find an example that was like Barmar suggested. Thank you – tcratius Sep 22 '17 at 07:39
  • This is really a double up, found a similar example, https://stackoverflow.com/questions/5404665/accessing-elements-of-python-dictionary. More than happy to delete this. – tcratius Sep 22 '17 at 07:45
  • Can I asked why I can't access ['fighter']['sword']['punch']? – tcratius Sep 22 '17 at 07:49
  • self.attack_moves_one = Human.attack_moves['fighter']['sword'] self.attack_moves_two = Human.attack_moves['fighter']['punch'] – tcratius Sep 25 '17 at 15:02

0 Answers0