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

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

Yatzy scoresheet challenge part 2

For this challenge, what I did was a key: value for loop to the dictionary of dice value and their amount in a set. I added an if statement saying if the amount was equal to 5 return 50, otherwise return 0. I didn’t use the worth variable in the for loop. Am I on the right track?

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):
        for worth, count in hand._sets.items():
            if count==5:
                return 50
            else:
                return 0

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

I think there are various solutions that will work. Essentially you are attempting to compare the values on the five die elements of the hand. You need to ask the question, are all the dice in the hand the same value.

Here's an example of something that could work.

def score_yatzy(self, hand):
    first_die_value = hand[0] # the value of the first die
    for current_die in hand:
        if current_die != first_die_value:
            return 0
    return 50