Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Inheritance Subclasses

Gilang Ilhami
Gilang Ilhami
12,045 Points

Goblin azog.hitpoints

When i typed in azoh.hitpoints, it returns 1 instead of 3

>>> from monster import Goblin
>>> azog = Goblin()
>>> azog.hit_points
1
import random

COLORS = ['yellow', 'red', 'blue', 'green']


class Monster:
  min_hit_points = 1
  max_hit_points = 1
  min_experience = 1
  max_experience = 1
  weapons = 'axe'
  sound = 'rawr'
  def __init__(self, **kwargs):
    self.hit_points = random.randint(self.min_hit_points, self.max_hit_points)
    self.experience = random.randint(self.min_experience, self.max_experience)
    self.color = random.choice(COLORS)

    for key, value in kwargs.items():
      setattr(self, key, value)

  def battlecry(self):
      return self.sound.upper()

class Goblin(Monster):
  max_hit_points = 3
  mas_experience = 2
  sound = 'squeak'

1 Answer

Here's the line where the hit_points are defined.

self.hit_points = random.randint(self.min_hit_points, self.max_hit_points)

If you piece in all the variables you have

self.hit_points = random.randint(1, 3)

In this case it will randomly pick hit_points to be either 1, 2 or 3. It's not broken that it's returning 1. Try it again and chances are you will get a different number there.

A normal Monster will always have hit_points=1. A 'Goblin isn't guaranteed to have hit_points=3, it just has that potential.