This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 8: Robots and remote clones

Descriptive robots

face Josiah Wang

As your final programming exercise for this lesson, let us revisit your robot project.

We will not add any new features this time, but instead perform a bit of refactoring, specifically on your robot representation itself.

Recall that earlier, we represented an employee as a structured dictionary representation. The dictionary represents the attributes of an employee.

employee = {"id": "14835634", "name": "Slađana Ellsworth", "age": 24, "nationality": "poland"}

If you recall, your robot can have a name, id, position, and direction. You will now try to represent your robot as a dict representation, with these attributes as keys.

robot = {"id": 1001, "name": "Daft Punk", "position": (7, 6), "direction": "s"}

You can now treat robot as a single object. Where relevant and suitable, you should then update any input parameters of your functions to take your robot dict as input, rather than taking in parameters like position and direction separately. Your functions can also return the robot dict directly as a whole, rather than only some aspects like position and direction. For example, if you have an initialise_robot() function, it can just return a single dict representation of a robot, rather than returning a tuple containing the different attributes.

You should also end up having a single list of dict representing each robot, rather than having separate lists to keep track of the robot’s name, position, direction, id independently (or you might have previously implemented this as a nested list?) You can alternatively index your robot dict objects by ID as what you did with the employee exercise - this will be down to your own design decision!

If you have trouble getting started, try starting at the ‘highest-level’ main code, and figure out whether there are any functions where it makes more sense to represent a robot as a single entity. Modify your functions one at a time; remember - incrementally test as you code, and try to get your code to stay in a ‘green’ state as much as possible!

This may feel like a chore now, but this will help make your code even more readable, and will help you tremendously in future lessons! If done correctly, your code should read like you are passing a robot all around your program, and using functions to manipulate a robot’s properties.

Remember to make sure that your code still works correctly. Also remember to commit any new changes to your repository when you are done!