Chapter 13: Classes and Objects¶
The answers on this page show ONE way to solve the exercises. However, there are usually OTHER ways to accomplish the same thing. This is OK!
A suggested solution is ONE way to solve the problem, not the ONLY way.
Chapter Sandbox¶
Use the editor below to test out any of the solutions!
Solutions¶
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.
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.
Add Properties to Robot
¶
Inside the class, define the
__init__
method. It should include parameters forself
,name
,mass
, andyear
.1 2 3 4 5 6
class Robot: #set up Robot Object Properties def __init__(self, name, mass, year): self.name = name self.mass = mass self.year = year
Give a default value to
year
.1
def __init__(self, name, mass, year = 1942):
Before you move on, test your new class! In
main()
:1 2 3 4 5 6 7 8 9 10
#a. Call the ``Robot`` class to create a new object. Assign it to a variable. robot_bob = Robot("Bob", 42, 1974) #Code to test creation of Robot class #b. Use dot notation to ``print`` the values for each property. print("\nRobot Information:") print(" Name: " + robot_bob.name) print(" Mass: " + str(robot_bob.mass) + "kg") print(" Year Made: " + str(robot_bob.year)) print(" Distance Traveled: " + str(robot_bob.distance))
Add Methods to Robot
¶
Below
__init__
, define a second method calledmove()
. This method should:1 2 3 4 5 6
#a. Only take the ``self`` parameter. def move(self): #c. Increase the ``distance`` property by the number of steps. steps_taken = random.randint(1,10) self.distance += steps_taken
Add the
__str__
method to return a string of the object properties.1 2 3 4
#returns a string of robot properties of our choosing def __str__(self): output = "\nRobot Info: \n Name: {0}\n Mass: {1} kg\n Year made: {2}\n Distance traveled: {3}" return output.format(self.name, self.mass, self.year, self.distance)
Test your methods! In
main()
:1 2 3
#a. ``print`` the ``Robot`` object to check the output. #Code that calls the __str__ method print(robot_bob)
Part 2: Create Objects¶
In part 1, you defined a class and created one Robot
object in main()
.
Now create three more objects:
Use a mass value from
25
to40
. (Bonus: Userandint
to generate the mass value instead of hard-coding a number when you call the class).1 2 3
robot_unimate = Robot("Unimate",random.randint(25, 40), 1954) robot_terry = Robot("Terry", random.randint(25, 40), random.randint(1923, 2022)) robot_jones = Robot("Jones", 42 , 1969)
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 calledrobots
.1
robots = [robot_bob, robot_unimate, robot_terry, robot_jones]
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.
1 2 3 #add in main for robot in robots: robot.distance = random.randint(1000, 3000)
Part 3: Find Oldest Robot¶
Between the class and main()
, define a function called oldest_robot
. It
should:
Use a loop to iterate through the list.
1 2 3 4 5 6 7 8
def oldest_robot(robot_list): robot_year = [] for robot in robot_list: robot_born = robot.year robot_year.append(robot_born) #index value of oldest robot in robot_list oldest = robot_year.index(min(robot_year))
If two robots have the same
year
value, then the one with the largestdistance
will be older.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#year oldest robot was made oldest_robot_year = robot_year[oldest] #checking to see if more then one robot born in oldest_robot_year same_year = robot_year.count(oldest_robot_year) #more then one robot born in oldest year if same_year > 1: #distance in steps of oldest robot oldest_robot_distance = robot_list[oldest].distance for index in range(len(robot_year)): year = robot_list[index].year if year == oldest_robot_year: if robot_list[index].distance > oldest_robot_distance: oldest_robot_distance = robot_list[index].distance oldest = index #Return the index value for the oldest robot in the list. return oldest
In
main()
, call theoldest_robot
function and userobots
for the argument. Assign the returned index to a new variable.1 2
#create variable and call function index_oldest = oldest_robot(robots)
Print out a message describing the result:
1 2
print(f"\n{robots[index_oldest].name} is the oldest robot (made in {robots[index_oldest].year}, {robots[index_oldest].distance} steps).\n")
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.1 2 3 4 5 6 7 8 9 10 11 12 13
#create empty list for results of each robot(part 4) results = [] #loop thru robots(part 1) for robot in robots: #set variables steps = 0 turns = 0 #keep taking turns until robot gets at least 30 steps(part 2-3) while steps <= 30: steps += robot.move() turns += 1 #add string with robot results to results list(part 4) results.append(f"{robot.name} took {turns} turns to take {steps} steps.")
Print the results to the console (one robot per line).
1 2 3 4 5 6
#in main #create variable and call function robot_race_results = robot_race(robots) for result in robot_race_results: print(result)