Welcome to the Treehouse Community
Looking to learn something new?
Carlos Marin7,997 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?
class YatzyScoresheet: def score_ones(self, hand): return sum(hand.ones) def _score_set(self, hand, set_size): scores =  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
Jeff MudayTreehouse Moderator 27,510 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, 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 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
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.