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 Object-Oriented Python Dice Roller RPG Roller

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

Need help on Python RPG Roller Challenge task 2

Hi all, I need some help figuring out this challenge. I'm at a loss. I've looked through all the other discussions regarding this challenge but, haven't come up with anything that helped me pass this challenge. There just seems to be a lot of confusion regarding this part of the challenge.

I'm hoping someone could walk me through the steps and potentially the path of logic to get the answer. I'd really appreciate it.

Cheo R
Cheo R
37,150 Points

As of right now, what do you now/understand?

3 Answers

Cheo R
Cheo R
37,150 Points

That's ok! Sometimes the hardest thing to do is understand what others want. When I don't understand something, I like to turn the prompt into comments and try to fill in what I know. For example:

import random

class Die:
    def __init__(self, sides=2):
        if sides < 2:
            raise ValueError("Can't have fewer than two sides")
        self.sides = sides
        self.value = random.randint(1, sides)

    def __int__(self):
        return self.value

    def __add__(self, other):
        return int(self) + other

    def __radd__(self, other):
        return self + other

# Create a new class in dice.py named D20
# that extends Die.
# It should automatically have 20 sides and
# shouldn't require any arguments to create.

So let's add onto he given code:

# Create a new class in dice.py named D20
class D20

# that extends Die.
class D20(Die)

# It should automatically have 20 sides and
class D20(Die):
    # what do i need to call here to make sure the die has 20 sides  ## hint, look at the Die class above

# shouldn't require any arguments to create.
  ## This is telling me it should be short code
Cheo R
Cheo R
37,150 Points

No worries. Sometimes it takes something awhile before it 'clicks'. When you get the chance, go over why it works.

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

Cheo R

class D20(Die): # The new class D20 and inherits its attributes from Die by placing the argument of Die in the ()s. 
    def __init__(self): # Doesn't need anything extra since it's inheriting attributes from Die class
       super().__init__(sides=20) # by using the super() method we create an instance of __init__ and override the sides of Die to be equal to 20

I hope I used the terminology correctly.

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

So, after starting off fresh and looking again through the answers to compare to what I had come up. I noticed that some people would say their code passed and even if it was similar to mine and I added what they had I still couldn't pass. Eventually, I stumbled upon this discussion. (https://teamtreehouse.com/community/rpg-roller-confusion) I noticed my code and his code were very close to being the same. I tested his code and it worked, but mine still didn't. Upon closer inspection, I realized he didn't have

self.sort()

I deleted mine and I passed. Could anyone explain why that is? Many people who passed had it in their code.

the code that passed:

class Hand(list):
    def __init__(self, size = 0, die_class = D20):
        super().__init__()
        for _ in range(size):
            self.append(die_class())
        #self.sort()

    @classmethod
    def roll(cls, size):
        return cls(size, die_class = D20)

    @property 
    def total(self):
        return sum(self)

The prompt requests a list, but doesn't explicitly say it needs to be sorted. As a result, when the code challenge tests the code you provide it, it gets a list back, which is what it is checking for, not whether or not it is sorted.

This is generally the case with Code Challenges. It is to account for the fact that people could obtain the same result with different methods and such in between. So it just checks for the end result.