Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Dice Roller Giving a Hand

NameError

As far as I'm aware this is his exact code at 2:00

class Hand(list):
    def __init__(self, size=0, die_class=None, *args, **kwags):
        if not die_class:
            raise ValueError("You must provide a die class")
        super().__init__()

    for _ in range(size):
        self.append(die_class())

import dice, hands
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/treehouse/workspace/pycache/hands.py", line 1, in <module>
class Hand(list):
File "/home/treehouse/workspace/pycache/hands.py", line 7, in Hand
for _ in range(size):
NameError: name 'size' is not defined

2 Answers

Megan Amendola
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

It looks like you have an instance attribute called size which means you need to access it with self. self.size

I tried changing that but it didn't work, then I set it to range(0), then back to range(size)....and now it works.....I'm so confused.

...and now it doesn't work again

it looks like you just have an indentation error. the for loop should be inside of the def init just bump the for loop in and that should work

class Hand(list): def init(self, size=0, die_class=None, *args, **kwargs): if not die_class: raise ValueError("You must provide a die class") super().init()

    for _ in range(size):  # <---- this needs to be even with the if and super
        self.append(die_class())