Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Dice Roller Comparing and Combining Dice

Idan shami
Idan shami
13,251 Points

Comparing and Combining Dice: TypeError, help please (:

I have the exact same code that Kenneth have but when I write d6 = D6() and then int(d6) like in the video it shows me this: *TypeError: int() argument must be a string, a bytes-like object or a number, not 'D6' * here is my whole code :

import random


class Die:
    def __init__(self, sides=2, value=0):
        if not sides >= 2:
            raise ValueError("Must have at least 2 sides")
        if not isinstance(sides, int):
            raise ValueError("Sides must be a whole number")
        self.value = value or random.randint(1, sides)

        def __int__(self):
            return self.value

        def __eq__(self, other):
            return int(self) == other

        def __ne__(self, other):
            return int(self) != other

        def __gt__(self, other):
            return int(self) > other

        def __lt__(self, other):
            return int(self) < other

        def __ge__(self, other):
            return int(self) > other or int(self) == other

        def __lt__(self, other):
            return int(self) < other or int(self) == other

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

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

class D6(Die):
    def __init__(self, value=0):
        super().__init__(sides=6, value=value)

help please ! (: have a good day.

Idan

David Luo
David Luo
1,214 Points

I think your code isn't wrong. It's what you entered into the workspace (or your IPython).

for example if you did the following:

input: d1 = D6

input: int(d1)

output: TypeError: int() argument must be a string, a bytes-like object or a number, not 'type'

Verses:

input: d1 = D6()

input: int(d1)

output: 3

I don't know the technical reason why d1 = D6 is wrong. Perhaps someone can shed some light.

Johannes Scribante
Johannes Scribante
19,175 Points

Idam, I want to mention 2 things

  1. Try looking at you indentation, from def __int__(self): is inside the def __init__(...)
  2. also look at less than or equals magic method you named it def __lt__ where it should def __le__

Then just one last point, based on David Luo's comment, as I understand it (I may be wrong here) but the reason why you assign a variable to a class using () is because otherwise python thinks it is a variable and does not know to go looking for a class.

input: d6 = D6
python: here python will go looking for a variable named D6

input: d6 = D6()
python: here python will go looking for a class named D6

Idan shami
Idan shami
13,251 Points

Sorry I understood this a long time ago but forgot to say it here.... I had some issues in my indentation like you said earlier Thanks anyway !! have a good day