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 __str__

Dragon is not defined

I'm getting this error telling me my subclasses are not defined. I know there's some stupid error. Please help me find it!

import random

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


class Monster:
  minHitpoints = 1
  maxHitpoints = 1
  minExperience = 1
  maxExperience = 1
  weapon = 'sword'
  sound = 'roar'

  def __init__(self, **kwargs):
    self.hitpoints = random.randint(self.minHitpoints, self.maxHitpoints)
    self.experience = random.randint(self.minExperience, self.maxExperience)
    self.color = random.choice(COLORS)

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

  def __str__(self):
    return '{} {}, HP: {}, XP: {}'.format(self.color.title(), 
                                          self.__class__.__name__, 
                                          self.hitpoints, 
                                          self.experience)

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




class Goblin(Monster):
  maxHitpoints = 3
  maxExperience = 2
  sound = 'squel'

class Troll(Monster):
  minHitpoints = 3
  maxHitpoints = 5
  minExperience = 2
  maxExperience = 6
  sound = 'growl'

class Dragon(Monster):
  minHitpoints = 5
  maxHitpoints = 10
  minExperience = 6
  maxExperience = 10
  sound = 'raaaaaaaaaar'
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

format help: please wrap your code in triple-backticks-python as mentioned in the Markdown Cheatsheet. I see you've used triple-single-quotes instead of backticks (next to the "1" on most keyboards).

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Only thing I see is that battlecry's content is indented 4 spaces while everything else is 2. Python is often really picky about indentation. Make them match and try again?

I was importing Monster instead of Dragon. My bad...thanks for the help though! Loving the python courses!!!