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 trialJames Gill
Courses Plus Student 34,936 Pointsno '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.
2 Answers
James Gill
Courses Plus Student 34,936 PointsSolved 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.
Kenneth Love
Treehouse Guest TeacherThe joy of name conflicts :)
Kevin Franks
12,933 PointsCan't be sure without seeing the code, but it sounds like your missing (from random import choice).
Mikael Enarsson
7,056 PointsMikael Enarsson
7,056 PointsCould you show us some sample code giving this error?