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 trialtaejooncho
Courses Plus Student 3,923 PointsWant explanation why my code is wrong for the chance scoring challenge
I looked at the previous video and found that count from hand._sets.items() will give how many dice have the same value.
if hand[:] = [one, one, one, one, one] hand._sets.items() will be [(1: 5), (2: 0), (3: 0), (4: 0), (5: 0)].
So I assumed that if I made count in hand._sets.items() == 5 it is equivalent to 5 dice with the same values.
Thus I wrote my code like the following. But when I try this code I get Bummer! Got the wrong score for Yatzy..
What am I failing to see?
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
Chris Freeman
Treehouse Moderator 68,457 PointsThis had me stumped for a bit. Think about what happens on the first iteration if the number of one's count is not five: 0 is returned and the other counts do not get checked.
move the return 0
outside of the for
loop so 0 is only returned if no Yatzy found
Andrew McLane
3,385 PointsAndrew McLane
3,385 PointsI thought that the for loop runs for every worth, count in _sets.items()? Why would it stop after the first iteration?
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsA
for
loop will run until either:break
statement is reachedreturn
statement is reachedOn the first iteration, the
if
statement the value ofcount
is compared to 5. If it is equal then 50 is returned.If it's not equal to 5, the
else
statement is run, and 0 is returned ending the loop.Moving the
return 0
outside thefor
loop will allow the loop to check every value pair then only return 0 if not count is 5.Post back if you need more help. Good luck!!!