# Object-Oriented Programming (Compar

My Code:

``````import math

class Team(object):
def __init__(self, team_name):
self.team_name = team_name
self.roster = []
for row in nba:
if row[3] == self.team_name:
self.roster.append(Player(row))
def num_players(self):
count = 0
for player in self.roster:
count += 1
return count
def average_age(self):
return math.fsum([player.age for player in self.roster]) / self.num_players()
# Define operators here
def __lt__(self, other):
return self.average_age() < other.average_age()
# Implement the rest of the comparison operators here
def __gt__(self, other):
return self.average_age() > other.average_age()

def __le__(self,other):
return self.average_age() <= other.average_age()

def __ge__(self,other):
return self.average_age() >= other.average_age()

def __ne__(self,other):
return self.average_age() != other.average_age()

jazz = Team("Utah Jazz")
pistons = Team("Detroit Pistons")

older_team = max([jazz,pistons])
``````

Hi guys, the above is my code for the OOP mission Page 8. I donβt have an issue with getting the correct answer but I need help understanding how and why the max function works.

1 Like

max() returns the maximum value among the arguments passed to the function.
Trying to find largest value among the objects of different types causes an error.
example:

max(10, βpypiβ)
Traceback (most recent call last):
File ββ, line 1, in
TypeError: unorderable types: str() > int()

max(5, [-10, 55])
Traceback (most recent call last):
File ββ, line 1, in
TypeError: unorderable types: list() > int()