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.

To start, fork the starter code on Replit.

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

  1. Inside the class, define the constructor. It should include parameters for self, name, mass, and year.

  2. Give a default value to year.

  3. Assign the property values as described in the Setting Property Values section.

  4. Add one more property called distance. This will not need to be included in the constructor. Instead, every new Robot object will start with a distance value of 0.

Tip

Before you move on, test your new class!

In main():

  1. Call the Robot class to create a new object. Assign it to a variable.

  2. Use dot notation to print the values for each property.

  3. To check the default, call the class at least once WITHOUT including an argument for year.

Check Your Solutions

Add Methods to Robot

  1. Below the constructor, define a method called move(). This method should:

    1. Only take the self parameter.

    2. Generate a random number of steps (1 - 10) for the object to take. The range should include 1 and 10 as options.

      Tip

      We are going to use an built-in function from the random module called randint(). This function will select a value from a range that you provide.

      The general syntax for module usage looks like this:

      module_name.item_name
      
      # in this instance, we will use the randint function like this:
      random.randint(start_value, end_value)
      

      We will explore modules more in future lessons.

    3. Increase the distance property by the number of steps.

    4. Return the random number of steps.

  2. 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():

  1. print the Robot object to check the output.

  2. Use a loop to call the move() method several times. Inside the loop, print the steps taken and the updated robot_name.distance value.

Check Your Solutions

Part 2: Create Objects

In part 1, you defined a class and created one Robot object in main(). Now create three more objects:

  1. Assign each Robot object to its own variable.

  2. Each object should have a different name value.

  3. Use a mass value from 25 to 40. (Bonus: Use randint to 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.

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.

Check Your Solutions

Part 3: Find Oldest Robot

Between the class and main(), define a function called oldest_robot. It should:

  1. Accept a list of robots as a parameter.

  2. Use a loop to iterate through the list.

  3. 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!

  4. If two robots have the same year value, then the one with the largest distance will 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.

Check Your Solutions

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:

  1. Each robot takes a turn running a race.

  2. A robot runs the race by calling its move() method several times.

  3. A robot is done with the race when it moves 30 steps or more.

  4. 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.

  5. Return the list from the function.

  6. 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.

Check Your Solutions