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

no 'choice' attribute

Throughout the Python OOP course, I'm getting this error:

AttributeError: 'module' object has no attribute 'choice'

The code at this point in the course:

import random

COLORS = ['yellow', 'green', 'brown', 'red', 'orange']


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 = 'squeak'

The key part, of course, is the import of 'random' and the call to random.choice.

Mikael Enarsson
Mikael Enarsson
7,056 Points

Could you show us some sample code giving this error?

2 Answers

Solved it, thanks to Stack Overflow: http://stackoverflow.com/questions/25695412/module-object-has-no-attribute-choice-trying-to-use-random-choice

I had a module named "random.py" in the directory already, and Python (understandably) threw the error. I renamed that file, and all is well. A lesson in good file naming.

Kevin Franks
Kevin Franks
12,933 Points

Can't be sure without seeing the code, but it sounds like your missing (from random import choice).