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

Chang Hyeon Lee
Chang Hyeon Lee
2,008 Points

I need help

import random

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

class Monster(object): 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_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 = 'hob'

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

class Dragon(Monster): max_hit_points = 8 min_hit_points = 9 min_experience = 5 max_experience = 2 sound = 'grall'

I wrote this quote and after I saved it and I typed the code:

import monster jake = monster.Dragon()

and I got this error

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/treehouse/workspace/monster.py", line 15, in init
self.hit_points =random.randint(self.min_hit_points, self.max_hit_points)
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/random.py", line 218, in randint
return self.randrange(a, b+1)
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/random.py", line 196, in randrange
raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (9,9, 0)

I don't understand what it is

help me

Chang Hyeon Lee
Chang Hyeon Lee
2,008 Points

I wrote Import monster /n jake = monster.Dragon()

1 Answer

Steven Parker
Steven Parker
231,271 Points

An error occurred in a different module.

This message is telling you that your monsters.py module called a random function but gave it no range to be able to pick a number from.

On one of your last lines above, you have this:

class Dragon(Monster): max_hit_points = 8 min_hit_points = 9 ...

You might need to pick values to insure that the maximum is not smaller than the minimum.