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

Andrei Oprescu
Andrei Oprescu
9,547 Points

The video had a confusing part, and now this challenge is confusing

When I watched the previous video In the objects course, it had a code like this:

def _sets(self): return { 1: len(self.ones), 2: len(self.twos), 3: len(self.threes), 4: len(self.fours), 5: len(self.fives), 6: len(self.sixes) }

I don't understand what is going on here. Have I skipped something?

Also, a challenge connected to this video asks:

Great! Let's make one more scoring method! Create a score_yatzy method. If there are five dice with the same value, return 50. Otherwise, return 0.

and the code for it is at the bottom of this comment.

Can someone tell me what the code in the video means and the solution for the challenge?

It would be really helpful

Thanks!

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):
        if sum(Hand) == Hand[0] * 5:
            return 50
        else:
            return 0

2 Answers

Steven Parker
Steven Parker
231,261 Points

You're pretty close there ...did you notice the error message: "Bummer! TypeError: score_yatzy() takes 1 positional argument but 2 were given". Like the other methods, the challenge is expecting "score_yatzy" to accept a "hand" argument. But that's one of a few issues:

  • pass the "hand" argument in to the new method
  • your code body is already looking for it, but it's spelled "Hand" instead of "hand"
  • remember that a "hand" contains a bunch of D6's. If you want to do math on one, use its value attribute

I'll bet you can get it now.

Erik Burmeister
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik Burmeister
Python Web Development Techdegree Graduate 17,108 Points

Steven Parker, I had an almost identical solution to Andrei. Would you mind explaining your 3rd bullet point a bit further? Where would you use value and how did you think to use value?

Steven Parker
Steven Parker
231,261 Points

In this challenge you can reference "Die" and "D6", but the code isn't shown here. You'd need to go back to a previous step and look at that code; and if you do, you 'll see that the "value" attribute is where the number value of a Die is kept.

To be able to do math directly on an object, that object must have several implicit methods ("dunder methods") set up for it to act like a number. But the value attribute already is a number.

Erik Burmeister
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik Burmeister
Python Web Development Techdegree Graduate 17,108 Points

Steven Parker, I see I thought that might be the case, but since the dice.py file wasn’t there I thought maybe it was a new variable that was added in this part of the challenge that I missed.

Andrei Oprescu
Andrei Oprescu
9,547 Points

Thank you! i understand what I did wrong now!