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 Comparing and Combining Dice

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

MY CODE:

import random

class Die: def init(self, sides=2, value=0): if not sides >=2: raise ValueError("Must have at least 2 sides or more") 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 __le__(self, other):
        return int(self) < other or int(self) == other

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

MY ERROR:

d1 = D6()
d2 = D6()
int(d1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'D6'

I saw someone else had the same issue but forgot to include the "def int(self): return self.value". I have that in there and it is still giving me the same error msg.

2 Answers

I also had this error and it was because my indentation block was too far in, make sure it's in line with the other methods within the class.

Steven Parker
Steven Parker
231,268 Points

This code seems to be OK, but it's hard to be sure with unformatted Python. Please use Markdown formatting so the code is shown as it is actually written.