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.5b

LO: To use conditional statements to solve problems.

Let's look at the code we have written so far having fixed our code to deal with two hurdles being close together:


#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():
    if front_is_clear():
        move()
    if wall_in_front():
        jump()

else:
    done()
    
			

We have seen how using if statements can help Reeborg check it's surroundings and respond accordingly. Which will surely be useful when we have to adjust our code again because of Hurdle Challenge 4!

Even before testing our existing code we can see that Reeborg is just not prepared to jump over hurdles of different sizes. We will need to use conditional statements to check how big our jump is.

Have a good look at these conditions that Reeborg can check so we can edit the jump() function to perform the correct jump height.

Taking a quick look at the world Hurdle Challenge 5 shows us that we are going to come across hurdles of all different sizes. The height of our jump will depend upon the height of the hurdle - as long as there is a wall to Reeborg's right it has to keep moving. We could rephrase this and say that while there is a wall to our right we have to keep moving...


#code we will keep

#define a function to jump
def jump():
    turn_left() #to face up
    ?
    turn_left() #to face right

			

Reeborg can check if there is a wall to the right by using the wall_on_right() condition. We can include this in a while loop:


#define a function to jump
def jump():
    turn_left() 
    while wall_on_right():
        move() #jump up the hurdle

    turn_left() 

			

It is worth remembering that once a condition is False in a while loop (there is no wall on the right) then the loop will break and code underneath the loop will be carried out. We want Reeborg to move over the top of the hurdle once it has reached the top.


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

    turn_right()
    move()
    turn_right() #Reeborg is facing down the hurdle

    turn_left() 

			

Now that Reeborg is facing down the other side of the hurdle we can use a while statement to move Reeborg until it reaches the floor.


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

    turn_right()
    move()
    turn_right()

    while front_is_clear():
        move() #move down until it reaches the floor

    turn_left() #turn to face the front

			

Putting this all together we have:


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

#define a function to jump
def jump():
    turn_left()
    #follow the wall as high as it will go
    while wall_on_right():
        move()
    #move over the top
    turn_right()
    move()
    turn_right()
    #follow the wall down
    while front_is_clear():
        move()
    turn_left()

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

else:
    done()
    
			

Try the code out on both the Hurdle Challenge 4 and 5 worlds.

Code Examples

Resources