EK 2020

Next summer, the Belgian Red Devils will play at the European Football Championship. In this task you will model a soccer team, where, just like in a soccer match, players can be changed for other players.

Excercise

Write a Python class SoccerTeam. This class represents a soccer team and should contain the following constructor and at least the following methods. You are completely free to choose the necessary instance variables and it is also allowed (and recommended!) to add additional methods, this can help you when looking for errors. Note that these methods use a SoccerPlayer class. You can download this here1, it is supposed to be placed locally in the same directory as the SoccerTeam class, but you do not have to submit it.

Specifications

Constructor

The constructor has 1 argument:

def __init__(self, name:str):

Methods

Your class must have at least the methods listed below. These are arranged in alphabetical order, not according to difficulty.

add_player()

def add_player(self, player:SoccerPlayer) -> bool:

This method adds the given player to the soccer team. If successful, the method returns True, otherwise False. It should only be possible to add a player if all the following conditions are met:

get_average_age()

def get_average_age(self) -> float:

This method returns the average age of all added players. When no players have been added yet, this method logically returns 0.0.

get_formation()

def get_formation(self) -> str:

This method determines the current formation of the soccer team. The formation is a string of 3 digits separated by dashes, indicating how many players are in each position. You can retrieve the position of a player with the get_position() method in the SoccerPlayer class. This method can have 4 possible outcomes:

The formation consists of the number of defenders, number of midfielders and number of attackers, in that order. For example, if a soccer team consists of 4 defenders, 4 midfielders and 2 attackers, this method will return the string 4-4-2.

get_name()

def get_name(self) -> str:

This method returns the name of the soccer team.

get_players()

def get_players(self) -> list:

This method returns an array of all players currently on the team. This array may contain null values; the test will remove them. The order of players in this array is not important.

get_players_at()

def get_players_at(self, position:SoccerPlayer.position) -> list:

This method returns an array of all players currently on the team playing at the given position position. This array may contain null values; the test will remove them. The order of players in this array is not important.

substitute()

def substitute(self, player_out:SoccerPlayer, player_in:SoccerPlayer) -> bool:

This method replaces the player player_out with the player player_in. If successful, the method returns True, otherwise False. A substitution may proceed only if the following conditions apply:

Remarks and tips

Testing locally

It is recommended that you try out your solution locally and optionally add some print statements to help you debug it. You can do this simply by writing code at the bottom of the file, outside the class and running the file.

team = SoccerTeam("Rode Duivels")
print(team.get_average_age()) # output: 0.0