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

Terry Sagas
Terry Sagas
2,354 Points

need help again

import random

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

class Monster: min_hit_points = 1 max_hit_points = 1 min_experience = 1 max_experience = 1 weapon = 'sword' sound = 'roar'

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_hit_experience)
    self.color = random.choise(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
    max_experience = 2
    sound = 'squeak'


class Troll(Monster):
    min_hit_points = 3
    max_hit_points = 5
    min_experience = 2
    max_experience = 6
    sound = 'growl'


class Dragon(Monster):
    min_hit_points = 5
    max_hit_points = 10
    min_experience = 6
    max_experience = 10
    sound = 'raaaaaaaaaaaaaar'

get an error like:                  

 import monster                                                                          Traceback (most recent call last):                                                            File "<stdin>", line 1, in <module>                                                         File "/home/treehouse/workspace/monster.py", line 6, in <module>                              class Monster:                                                                            File "/home/treehouse/workspace/monster.py", line 26, in Monster                              class Goblin(Monster):                                                                  NameError: name 'Monster' is not defined                                                   

1 Answer

Hi there terry, first of all, did you change the code like my answer stated ? your code should look something like this:

import random

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

class Monster: min_hit_points = 1 max_hit_points = 1 min_experience = 1 max_experience = 1 weapon = 'sword' sound = 'roar'

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_hit_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
    max_experience = 2
    sound = 'squeak'


class Troll(Monster):
    min_hit_points = 3
    max_hit_points = 5
    min_experience = 2
    max_experience = 6
    sound = 'growl'


class Dragon(Monster):
    min_hit_points = 5
    max_hit_points = 10
    min_experience = 6
    max_experience = 10
    sound = 'raaaaaaaaaaaaaar'

Please comment if answer worked.

P.S, after modifying your code a bit, here's the correct version of the code:

import random

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

class Monster:
    min_hit_points = 1
    max_hit_points = 1
    min_experience = 1
    max_experience = 1
    weapon = 'sword'
    sound = 'roar'


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_hit_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
    max_experience = 2
    sound = 'squeak'


class Troll(Monster):
    min_hit_points = 3
    max_hit_points = 5
    min_experience = 2
    max_experience = 6
    sound = 'growl'


class Dragon(Monster):
    min_hit_points = 5
    max_hit_points = 10
    min_experience = 6
    max_experience = 10
    sound = 'raaaaaaaaaaaaaar'

notice how in the Monster , the code is indented properly. If you're working at home, I'd recommend using a text editor and installing a PEP8 plugin. If you don't wish to use a text editor, I recommend you using the pycharm (Which is what im using for my work), it comes with great features and has PEP8 built-in. Good luck :)

Terry Sagas
Terry Sagas
2,354 Points

thank you so much it helped a lot

No problem my friend :) We're all here to help