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 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())