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

Barak Eyalb
PLUS
Barak Eyalb
Courses Plus Student 983 Points

Got the wrong score for a non-Yatzy

not sure why my method doesnt work...

scoresheets.py
class YatzyScoresheet:
    def score_ones(self, hand):
        return sum(hand.ones)

    def _score_set(self, hand, set_size):
        scores = [0]
        for worth, count in hand._sets.items():
            if count == set_size:
                scores.append(worth*set_size)
        return max(scores)

    def score_one_pair(self, hand):
        return self._score_set(hand, 2)

    def score_chance(self, hand):
        return sum(hand)

    def score_yatzy(self, hand):
        if sum(hand) == 0:
            return 50
        elif sum(hand) == 5:
            return 50
        elif sum(hand) == 10:
            return 50
        elif sum(hand) == 15:
            return 50
        elif sum(hand) == 20:
            return 50
        elif sum(hand) == 25:
            return 50
        elif sum(hand) == 30:
            return 50
        else:
            return 0

1 Answer

Oskar Lundberg
Oskar Lundberg
9,534 Points

Here is my solution:

class YatzyScoresheet:
    def score_ones(self, hand):
        return sum(hand.ones)

    def _score_set(self, hand, set_size):
        scores = [0]
        for worth, count in hand._sets.items():
            if count == set_size:
                scores.append(worth*set_size)
        return max(scores)

    def score_one_pair(self, hand):
        return self._score_set(hand, 2)

    def score_chance(self, hand):
        return sum(hand)

    def score_yatzy(self, hand):
        if self._score_set(hand, 5) > 0:
            return 50
        return 0

One reason to why your current method doesn't work is that sum([1, 1, 1, 1, 6]) would be equal to 10, and therefore return 50. Instead, use self._score_set(hand, 5). Hope this helps :D