13.12. Project: Classes and Objects¶
Now its time to design a new class of robots. These objects will all be able to clean our school building, but we want to pick the best ones.
Let’s create a class to handle new cleaning crew candidates!
13.12.1. Before You Start¶
If your teacher added you to a Trinket course, login to your account to access the starter code for this project.
Otherwise, use the links below to copy the starter code.
Trinket code (Remix before getting started).
Repl.it code (Fork before getting started).
13.12.2. Part 1: Add Class Properties and __str__
Method¶
Declare a class called
CrewCandidate
with an__init__
that takes three parameters:name
,mass
, andscores
. Note thatscores
will be a list of test results for the candidate’s speed and accuracy.In
main()
, create objects for the following candidates:iClean has a mass of 13.5 kg and test scores of 88, 85, and 90.
Shiny has a mass of 1.5 kg and test scores of 93, 88, and 97.
DustVac has a mass of 22.5 kg and test scores of 75, 78, and 62.
Define a
__str__
method to display the properties of each candidate. The output should look something like:**Candidate Information** Name: Shiny Mass: 1.5 kg Scores: [93, 88, 97]
Print each object to check to see if your class correctly assigns and displays the property values. (You can remove or comment out the
print
statement after your tests pass).
13.12.3. Part 2: Add Another Class Method¶
As our candidates complete more tests, we need to be able to add the new scores to their records.
Create an
add_score
method inCrewCandidate
. The function must take a new score as a parameter in addition toself
.When passed a score, the function adds the value to
self.scores
with the append method.Tip
Inside the class, the name of the list is
self.scores
instead ofscores
. Thus,self.scores.append()
is the proper syntax.In
main()
, test your new method by adding a score of83
to iClean’s record. Print out the new score list withobject_name.scores
.
13.12.4. Part 3: Add More Methods¶
Now that we can add scores to our candidates’ records, we need to evaluate
their fitness for our cleaning program. Let’s add two more methods to
CrewCandidate
. One will average the test scores, and the other will decide
if the candidate should be added to the cleaning team.
13.12.4.1. Calculating the Test Average¶
Define an
average()
method. It only needs theself
parameter.To find the average, add up the entries from
self.scores
, then divide the sum by the number of scores in the list.To make the average easier to look at, round it to 1 decimal place, then return the result from the method.
Check your code by evaluating and printing Shiny’s average test score
(92.7
).
13.12.4.2. Determining Candidate Status¶
Candidates with averages at or above 90% are automatically added to our new cleaning crew. Backup robots average between 80 - 89%, while robots with averages between 70 - 79% get sent out for repairs. Averages below 70% lead to a short trip to the recycling bin.
Add a
status()
method toCrewCandidate
. The method returns a string ('Accepted'
,'Backup'
,'Maintenance'
, or'Scrapped'
) depending on a candidate’s average.The
status
method requires the average test score. Fortunately, methods can call other methods inside a class! Just remember to useself.method_name()
.Once
status
has a candidate’s average score, return the proper string based on that value.In
main()
, test thestatus
method on each of the three candidates. Print the result with the format,'___ scored an average of ___% (___).'
iClean scored an average of 87.7% (Backup). Shiny scored an average of 92.7% (Accepted). DustVac scored an average of 71.7% (Maintenance).
13.12.5. Part 4: Play a Bit¶
Use the class methods to boost DustVac’s status to Backup
or higher. How
many good tests will it take to reach Backup
status? How many to reach
Accepted
?
Note that scores cannot exceed 100%.
Tip
Rather than adding one score at a time, could you use a loop?