Python Object-Oriented Python Dice Roller Chance Scoring

Carlos Marin
Carlos Marin
Python Web Development Techdegree Student 5,344 Points

Yatzy help!!! I not sure if I understand the objective quite clearly.

I am checking all of the dice values and checking to see if any of them were rolled 5 times. if so, then I return the integer 50. else, I return 0. am I misunderstanding the objective, or is my code faulty?

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):
        list_of_die_values = []
        for worth, quantity in hand._sets.items():
            list_of_die_values.append(worth*quantity)
        return sum(list_of_die_values)

    def score_yatzy(self, hand):
        five_point_combo = 0
        for _ in hand._sets.values():
            if _ == 5:
                five_point_combo = 50
            else:
                five_point_combo = 0
        return five_point_combo

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 23,526 Points

Since we know that in order to score a yatzy, all the dice must be the same as the first one. And the hand inherits from the native Python list object, it is pretty easy to compute. We can get the value of the first die in the hand by accessing hand[0], then we check the every die in the hand to see if it equals the first die.

The weakness is that we don't check if the hand is 5 dice, but one could add that check too.

Good luck with Python!

    def score_yatzy(self, hand):
            first_die = hand[0]
            for value in hand:
                # if any dice are different from the first_die, return 0
                if value != first_die:
                    return 0
            # we know all dice are the same, return a 50
            return 50
KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 50,936 Points

You could do it the way you have it as well. However in your loop you will only want the if statement. Since you loop through values for 1 to 6 if you include the else statement, you'd only score 50 if the hand was 5 sixes. Any other Yatzy and five_point_combo would be set to 0 on the final iteration. So remove the else statement and you should be good.

Carlos Marin
Carlos Marin
Python Web Development Techdegree Student 5,344 Points

Hey KRIS, thanks for the information. Makes total sense, glad I got quick help.