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 trialIdan shami
13,251 PointsComparing 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
Johannes Scribante
19,175 PointsIdam, I want to mention 2 things
- Try looking at you indentation, from
def __int__(self):
is inside thedef __init__(...)
- also look at less than or equals magic method you named it
def __lt__
where it shoulddef __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
13,251 PointsSorry 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
David Luo
1,215 PointsDavid Luo
1,215 PointsI 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.