13.11. Exercises: Classes and Objects¶
In these exercises, you will create a Robot class and use it to create
four Robot objects. You will also practice writing functions that display
information about the objects and race them against each other.
Note
If your teacher added you to a Trinket course, login to your account to access the starter code for each exercise.
Otherwise, use the links below to copy the starter code.
Trinket starter code (Remix before getting started).
Repl.it starter code (Fork before getting started).
13.11.1. Part 1: Create a New Class¶
Open up the starter code and notice that it imports the random module.
Below the import statement, define the Robot class.
13.11.1.1. Add Properties to Robot¶
Inside the class, define the
__init__method. It should include parameters forself,name,mass, andyear.Give a default value to
year.Assign the property values as described in the Setting Property Values section.
Add one more property called
distance. This will not need a parameter in the__init__definition. Instead, every newRobotobject will start with adistancevalue of0.
Tip
Before you move on, test your new class!
In main():
Call the
Robotclass to create a new object. Assign it to a variable.Use dot notation to
printthe values for each property.To check the default, call the class at least once WITHOUT including an argument for
year.
13.11.1.2. Add Methods to Robot¶
Below
__init__, define a second method calledmove(). This method should:Only take the
selfparameter.Generate a random number of steps (1 - 10) for the object to take. The range should include
1and10as options. Hint: Therandintfunction is described in the Random Module section.Increase the
distanceproperty by the number of steps.Return the random number of steps.
Add the
__str__method to return a string of the object properties.print(robot_name)should produce something like:Robot information: Name: Bob Mass: 25 kg Year made: 2020 Distance traveled: 0
Tip
Test your methods!
In main():
printtheRobotobject to check the output.Use a loop to call the
move()method several times. Inside the loop,printthe steps taken and the updatedrobot_name.distancevalue.
13.11.2. Part 2: Create Objects¶
In part 1, you defined a class and created one Robot object in main().
Now create three more objects:
Assign each
Robotobject to its own variable.Each object should have a different
namevalue.Use a mass value from
25to40. (Bonus: Userandintto generate the mass value instead of hard-coding a number when you call the class).
You now have 4 total robots. Add another statement in main() where you
place the objects inside a list. Assign the collection to a variable called
robots.
13.11.2.1. Update Distances¶
Use a loop to iterate through the robots list. For each object, assign a
random value to the distance property, from 1000 to 3000 steps.
13.11.3. Part 3: Find Oldest Robot¶
Between the class and main(), define a function called oldest_robot. It
should:
Accept a list of robots as a parameter.
Use a loop to iterate through the list.
Return the index value for the oldest robot in the list. Caution: A robot made in 2020 is NOT older than one made in 2019!
If two robots have the same
yearvalue, then the one with the largestdistancewill be older.
Note
The instructions assume that the list contains only one oldest robot. What
if two old robots have the same year and distance values?
We won’t solve this problem here. However, add comments in your code describing how you would deal with multiple robots that are the same age.
In main(), call the oldest_robot function and use robots for the
argument. Assign the returned index to a new variable.
Print out a message describing the result:
"___ is the oldest robot (made in ___, ___ steps)."
Fill in the blanks with the name of the robot, the year it was made, and its current number of steps.
13.11.4. Part 4: Robot Races¶
Now it’s time for the robots to compete against each other! Define the
robot_race function that takes a list of robots as a parameter.
Within the function:
Each robot takes a turn running a race.
A robot runs the race by calling its
move()method several times.A robot is done with the race when it moves 30 steps or more.
Create a new list to store how many turns it takes each robot to complete the race. Use the string:
'____ took ____ turns to take 30 steps.'Fill in the blanks with the robot’s name and race result.Return the list from the function.
Print the results to the console (one robot per line).
Bob took 7 turns to take 30 steps. Jen took 5 turns to take 30 steps. Sal took 7 turns to take 30 steps. Jessi took 6 turns to take 30 steps.
HINT: There are a lot of different ways to approach this problem. One way
that works well is to count how many times move() gets called in order to
reach 30 steps.
