Search⌘ K

Solution Review 2: Implementing a Sports Team!

Explore how to model real-world object relationships in Python using classes for Player, Team, and School. Learn to manage lists of objects, add players to teams, teams to schools, and calculate total players, gaining practical experience in object-oriented programming concepts.

We'll cover the following...

Solution #

Python 3.5
class Player:
def __init__(self, ID, name, teamName):
self.ID = ID
self.name = name
self.teamName = teamName
class Team:
def __init__(self, name):
self.name = name
self.players = []
def getNumberOfPlayers(self):
return len(self.players)
def addPlayer(self, player):
self.players.append(player)
class School:
def __init__(self, name):
self.name = name
self.teams = []
def addTeam(self, team):
self.teams.append(team)
def getTotalPlayersInSchool(self):
length = 0
for n in self.teams:
length = length + (n.getNumberOfPlayers())
return length
p1 = Player(1, "Harris", "Red")
p2 = Player(2, "Carol", "Red")
p3 = Player(1, "Johnny", "Blue")
p4 = Player(2, "Sarah", "Blue")
red_team = Team("Red Team")
red_team.addPlayer(p1)
red_team.addPlayer(p2)
blue_team = Team("Blue Team")
blue_team.addPlayer(p2)
blue_team.addPlayer(p3)
mySchool = School("My School")
mySchool.addTeam(red_team)
mySchool.addTeam(blue_team)
print("Total players in mySchool:", mySchool.getTotalPlayersInSchool())
  • Line 2 – 5: Defined initializer for Player

  • Line 9 – 11: Defined initializer for Team, which also contains a list of players

  • Line 13 – 14: ...