Table Of Contents

Previous topic

9. Rule number 3

Next topic

11. Using the Library

This Page

10. Turn around

In addition to having Reeborg making right turns, you might have had him turn around a few times; by turning around, I mean changing direction so that Reeborg is heading back to where it was coming from. We can define a function turn_around() as follows:

def turn_around():

Try it!

10.1. Back to turn_right()

Remember turn_right()? Here it is again:

def turn_right():

Notice how the first two instructions are identical to the function definition for turn_around(). When this happens, we have to remember Rule # 3:


Rule # 3
When writing computer programs, do not repeat yourself. I repeat: do not repeat yourself!

So, we have some repeated instruction. While turn_right() is already very simple, a good programming practice is to replace parts of code that are repeated by a simple function. Thus, we should rewrite turn_right() as follows:

def turn_right():

The idea behind this is that, the shorter a function is, the least likely there will be bugs in it. Furthermore, once we have a well-tested bug-free function, we make sure that we use it whenever we can when writing longer functions. I admit, this is almost a silly example ... but I don’t have more complicated examples at this point to illustrate this important idea.

Your turn

Define a step_back() function that would undo a move() function. By this, I mean that you should have:

# starting somewhere at location x,y
# back at the same location,
# facing in the same direction as before

Make sure you to test it!

Do not use the hint unless you feel you absolutely can not do it otherwise!


You might want to use turn_around() twice in defining step_back().