In this lab, we'd like you to make some classes of your own. Below we've outlined a few scenarios to be modelled as classes.
We have provided a set of files where you can create your classes and test your code.
- Be able to create your own class
- Be able to create objects
- Be able to add properties to a class
- Be able to access and modify properties
- Be able to add methods to a class
- Be able to modify the value of properties in methods
For this part we want you to make a class that represents a CodeClan student.
The files for this section can be found in single_class_lab_start_code/student_class
You should write your code in the the src/student.py
file.
The tests we have provided are in tests/student_test.py
They are all skipped at the moment. You should work through the tests one at a time, deleting the line which causes the test to skip.
To run the tests:
python3 run_tests.py
- Create a class called Student that has the properties
name
(str
) andcohort
(str
). - Create an
__init__
method that takes in a name (str
) and a cohort (str
- e.g "E41", "G19", etc) to initialise the properties when a new student is created. - Create a
talk
method that gets the student to talk, returning "I can talk!". - Create a method,
say_favourite_language
that takes in a students favourite programming language and returns it as part of a string (eg.student.say_favourite_language("Python")
-> "I love Python").
Now we would like you to make a class that represents a sports team.
The files for this section can be found in single_class_lab_start_code/team_class
You should write your code in the the src/team.py
file.
The tests we have provided are in tests/team_test.py
They are all skipped at the moment. You should work through the tests one at a time, deleting the line which causes the test to skip.
To run the tests:
python3 run_tests.py
- Create a class called Team that has the properties properties
name
(str
),players
(list ofstr
s) and acoach
(str
). - Create an
__init__
method that takes in a name (str
), a list of player names (asstr
s) and a coach(str
) to initialise the properties when a new team is created. - Create a method
add_player
that takes in a string of a new players's name and adds that new player to the players list. - Add a method
has_player
that takes in a string of a player's name and checks to see if they are in the players list. It should returnTrue
if the player's name is in the list andFalse
otherwise.
-
Add a points property into your class that starts at 0 without adding it as a parameter inside your init function.
-
Create a method,
play_game
that takes in whether the team has won(True
) or lost(False
) and adds 3 to the points property for a win.