Python Object-Oriented Python Dice Roller RPG Roller

Erik Burmeister
Erik Burmeister
Python Web Development Techdegree Student 6,149 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
35,541 Points

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

Erik Burmeister
Erik Burmeister
Python Web Development Techdegree Student 6,149 Points

Cheo R, honestly, after seeing all the posts about this question I became more confused. I can't say I understand much at all.

3 Answers

Cheo R
Cheo R
35,541 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
Erik Burmeister
Erik Burmeister
Python Web Development Techdegree Student 6,149 Points

Cheo R

class D20(Die):
    def __init__(self):
        super().__init__(sides=20) 

Btw thanks for responding so fast. I didn't expect an answer anytime soon.

Cheo R
Cheo R
35,541 Points

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

Erik Burmeister
Erik Burmeister
Python Web Development Techdegree Student 6,149 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
Erik Burmeister
Python Web Development Techdegree Student 6,149 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)