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 Intro to Inheritance

UnboundLocalError: local variable 'random' referenced before assignment - Why is this happening?

''' import random

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

class Monster: min_hit_points = 1 max_hit_points = 1 min_expereince = 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_expereince, self.max_experience)
    self.color = random = random.choice(COLORS)

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

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

'''

2 Answers

I don't know why, but you for some reason wrote this:

self.color = random = random.choice(COLORS)

Anyway, you were supposed to write this:

self.color = random.choice(COLORS)

It should work. :smile:

~Alex

Hello

You appear to have several typos in your code. I suspect if you fix the typo's you will be ready to continue

COLORS = ['yellow','red','green' 'blue', 'green']
class Monster():
    min_hit_points = 1
    max_hit_points = 1
    min_expereince = 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_expereince, self.max_experience)
    self.color = random.choice(COLORS)    #---- you had a typo here

    for key, value in kwargs.items():  #---- you had a typo here
        setattr(self,key,value)                      

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

Hope this get you going ... I am not sure what the challenge is asking for ... it been a while for me, but fixing these 2 typos es will remove the syntax errors