Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Create a Team Composed of Players and a Coach

Model a sports Team using composition: build Player and Coach objects and compose them into a Team with useful behaviors.

Python practice15 minComposition & Real-World ModelingIntermediateLast updated April 11, 2026

Problem statement

You will model a simple sports Team using composition (Team contains Player and Coach objects). Implement three classes: Player, Coach, and Team. The Team should manage a collection of Player instances and a single Coach instance. Provide methods to add and remove players, assign a coach, compute total salary, list the roster, find players by position, and transfer a player to another team. Design requirements (high level): - Player: stores name, position, salary. - Coach: stores name and years of experience. - Team: has a name, a list of Player objects, and optionally a Coach. Team manages players and uses Coach for coach-related queries. Write clean, well-encapsulated methods so external code interacts with Team rather than manipulating internals directly.

Task

Practice designing classes that compose other objects. Implement methods to manage players and a coach, calculate total salary, query roster information, and transfer players between teams.

Examples

Creating a team, adding players and coach

Input

t = Team('Rovers') t.add_player(Player('Alice', 'Forward', 50000)) t.set_coach(Coach('Sam', 8)) (t.get_roster(), t.total_salary(), t.coach_experience())

Output

(['Alice'], 50000, 8)

Team holds a player and a coach. get_roster returns player names in add order, total_salary sums player salaries, coach_experience returns the coach's years of experience.

Input format

There is no stdin. Implement classes and functions as described. Tests will call helper functions or methods and evaluate returned values.

Output format

Functions and methods should return Python values. Tests compare the string form of returned values to expected strings.

Constraints

Do not use external libraries. Keep data structures simple (lists for player storage). Preserve insertion order for roster-related methods.

Samples

Sample 1

Input

sample_team_total()

Output

150000

sample_team_total builds a team with two players (50000 and 100000) and returns the total salary.