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

Simon Amz
Simon Amz
4,606 Points

Bummer 'Try again' for creation of a new instance

Hi,

In my code below, for the ast question, I have to create a new instance of the class. In my text editor all works perfectly:

Here is the main: hand = CapitalismHand() print(hand) hand[:] = [2,2] print(hand) print(hand._sets) print(hand.doubles) print(CapitalismHand.reroll(hand))

I got the new instance (rerolling the dice) with 2 D6,however I got this error et I don't know where it comes from.

Thanks for your help

hands.py
from dice import D6


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):
            self.append(die_class())
        self.sort()

    def _by_value(self, value):
        dice = []
        for die in self:
            if die == value:
                dice.append(die)
        return dice


class CapitalismHand(Hand):
    def __init__(self, *args, **kwargs):
        super().__init__(size=2, die_class=D6, *args, **kwargs)

    @property
    def ones(self):
        return self._by_value(1)

    @property
    def twos(self):
        return self._by_value(2)

    @property
    def threes(self):
        return self._by_value(3)

    @property
    def fours(self):
        return self._by_value(4)

    @property
    def fives(self):
        return self._by_value(5)

    @property
    def sixes(self):
        return self._by_value(6)

    @property
    def _sets(self):
        return {
            1: len(self.ones),
            2: len(self.twos),
            3: len(self.threes),
            4: len(self.fours),
            5: len(self.fives),
            6: len(self.sixes)
        }

    @property
    def doubles(self):
        for key, die in self._sets.items():
            if die == 2:
                return True
        return False

    @classmethod
    def reroll(cls, hand):
        if hand.doubles:
            new_hand = CapitalismHand()
        return new_hand

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

The challenge wording is not clear. There isn't a need to take a hand argument in the reroll() method, nor is there a need to verify if doubles had actually been rolled. Simply return CapitalismHand()

Post back if you need more help. Good Luck!

Siri Ramos
Siri Ramos
3,107 Points

Wow that's seriously unclear. I actually tried both ways:

1) Creating reroll function to return ONLY a new instance and then calling it in the doubles function if any doubles existed (following the first part of the prompt)

2) Creating reroll function to check if doubles exist using the doubles function and if there were doubles, then return a new instance.

Turns out both were wrong. Terrible wording!

Simon Amz
Simon Amz
4,606 Points

Indeed, the wording is completely ambiguous, and not really interesting as the check if the dice is double is not made by us.

Actually the classmethod reroll only return a new instance, but it doesn't make the check of the 'doubles' property. the automatic corrector must make the verification and then call our class method 'reroll' in order to create a new instance.

Thanks Chris for your help