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

Jeremy Kerrigan
Jeremy Kerrigan
12,002 Points

[Solved] import monster does not work and I cannot call my Goblin() subclass.

Why doesn't my import monster work on my Goblin class. It gives me this error:

treehouse:~/workspace$ python
Python 3.5.0 (default, Feb 15 2016, 01:59:50)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.

import monster_5
azog = monster.Goblin()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'monster' is not defined

import random

COLORS = ['yellow', 'red', 'green', 'blue']
WEAPONS = ['axe', 'bow and arrow', 'ninja stars']
class Monster:
    min_hit_points = 1
    max_hit_points = 1
    min_experience = 1
    max_experience = 1
    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_experience)
        self.color = random.choice(COLORS)
        self.weapon = random.choice(WEAPONS)

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


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

class Goblin(Monster):
    max_hit_point = 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 = 'raaarrr'

The name of my saved file in workspaces is monster_5.py

Never mind..figured it out. I also have to mention monster_5.Goblin() when creating the azog character

azog = monster_5.Goblin()

1 Answer

Steven Parker
Steven Parker
229,784 Points

Congratulations on resolving your own issue :+1:

You could edit your title now and preface it with [SOLVED].