design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.

use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs

5.4b

LO: To use conditional statements to solve problems.

Let's look at the code we have written so far:


#use a loop to check if Reeborg is at the goal
while not at_goal():
    move()
    if wall_in_front():
        turn_left()
        move()
        turn_left()
        turn_left()
        turn_left()
        move()
        turn_left()
        turn_left()
        turn_left()
        move()
        turn_left()

else:
    done()
    
			

We can improve our code, make it more readable, by using functions as we did back in 3.5a, this would look like:


#define a function to jump
def jump():
    turn_left()
    move()
    turn_left()
    turn_left()
    turn_left()
    move()
    turn_left()
    turn_left()
    turn_left()
    move()
    turn_left()

#use a loop to check if Reeborg is at the goal
while not at_goal():
    move()
    if wall_in_front():
        jump()

else:
    done()
    
			

You can see that there is still code repetition so we can use another function (notice that we define these before the while loop):


#define a function to turn right
def turn_right():
    turn_left()
    turn_left()
    turn_left()

#define a function to jump
def jump():
    turn_left()
    move()
    turn_right()
    move()
    turn_right()
    move()
    turn_left()

#use a loop to check if Reeborg is at the goal
while not at_goal():
    move()
    if wall_in_front():
        jump()

else:
    done()
    
			

This code works just as well but it is clearer to see what is going on. Just to make sure, try the new code on the world Hurdle Challenge 3.

Ouch!

We can see Reeborg has encountered a problem - when the hurdles are close together it hits a wall. Can you see in the code where the error has been made? Remember to use the step command so you can see each instruction Reeborg follow and in what order.

The problem seems to occur when Reeborg executes the move() command, let's just look at the game loop and think about what Reeborg is doing:


#use a loop to check if Reeborg is at the goal
while not at_goal():
    move()
    if wall_in_front():
        jump()

else:
    done()
    
			

If Reeborg is not on the goal/home square then it will:

Is the problem in the order or instructions? Should Reeborg check if there is a wall before moving? Let's make the changes and try:


#define a function to turn right
def turn_right():
    turn_left()
    turn_left()
    turn_left()

#define a function to jump
def jump():
    turn_left()
    move()
    turn_right()
    move()
    turn_right()
    move()
    turn_left()

#use a loop to check if Reeborg is at the goal
# check before moving!
while not at_goal():
    if wall_in_front():
        jump()
    move()

else:
    done()
    
			

Ouch!

The problem is still there! When Reeborg gets to square (7,1) it checks if there is a wall in front (there is) so it jumps and then moves - but there is a wall in front! We can see that the order of the instructions has not helped us. If only there was a way of checking if the front is clear and only moving if it is True...

Try correcting the code yourself before looking at the Hurdle Challenge 3 code example below.

Code Examples

Resources