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

NameError: name 'D6' is not defined

from Project_dice import dice

class Hand(list):
dice_class (=what type of dice will be used --> 2sided,4sided,6sided...)
    def __init__(self, size=0, dice_class=None, *args, **kwargs):
        if not dice_class:
            raise ValueError("You must provide a dice class")
        super().__init__()

        for _ in range(size):
            self.append(dice_class())
        # will sort Dice instances in current hand (list) by its value (since the __gt__ and __lt__ are defined for Dice class)
        self.sort()


hand1 = Hand(size=5, dice_class=dice.D6)


class YatzyHand(Hand):
    def __init__(self, *args, **kwargs):
        super().__init__(size=5, dice_class=D6, *args, **kwargs)

yatzy = YatzyHand()

This is my code, for some reason im getting error:

Traceback (most recent call last):
line 22, in __init__
    super().__init__(size=5, dice_class=D6, *args, **kwargs)
NameError: name 'D6' is not defined

If I define the YatziHand's super().init like this, its working fine:

super().__init__(size=5, dice_class=dice.D6, *args, **kwargs)

So my question is: why do I have to specify dice_class=dice.D6 , while he can use simply dice_class=D6 ??

1 Answer

Steven Parker
Steven Parker
243,253 Points

Add this :point_right: from Project_dice import D6