Prateek Sharan Lall1,777 Points
TypeError: '<' not supported between instances of 'D6' and 'D6'
While running, I am getting this error. Please help, I cannot find what did I do wrong. Thanks.
from dice import D6 class Hand(list): def __init__(self, size=0, die_class=None, *args, **kwargs): if not die_class: raise ValueError("You must provide a die class") super().__init__() for _ in range(size): self.append(die_class()) self.sort() class YatzyHand(Hand): def __init__(self, *args, **kwargs): super().__init__(size=5, die_class=D6, *args, **kwargs)
Steven Parker203,137 Points
Chris's suggestion will work, but in the video, a "
__lt__" method is implemented in the "Die" class, which is the base (parent) class of D6. So it's not necessary to re-implement it in D6.
But check your "Die" implementation (in "dice.py", not shown here) in case that method was overlooked or has an error.
Chris FreemanTreehouse Moderator 59,730 Points
TypeError issue is being raised by the line
list.sort method needs a way to compare two items. The default method is to use the less than
< operator. This requires the
__lt__ method to be present in each object contained in the
list. There are two choices:
__gt__ method to the D6 class
def __lt__(self, other): return int(self) < other def __gt__(self, other): return int(self) > other def __repr__(self): """Allow seeing value instead of object description""" return str(int(self))
key= parameter to the
self.sort(key=lambda x: x.value)
Using the key argument says "Use this function, which returns the attribute
int), when comparing items ".
Post back if you need more help. Good luck!!