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
LO: To use conditional statements to solve problems.
We are going to return to the Hurdle worlds that we first looked at in 3.3a, back then we looked at putting repeated code in functions, now we are going to look at using conditional statements to solve problems.
Let's start with a simple example using the hurdleChallenge1 world
Look again at the conditions that Reeborg is aware of:
What conditions can we make use of to allow Reeborg to jump the hurdle and end on the home/goal?
We can start by using our game loop and include an if statement:
#use a loop to check if Reeborg is at the goal while not at_goal(): #do something if wall_in_front(): #jump else: done()
You may want to look back at 3.3a and the code we wrote back then.
#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()
Now try the same code on the world Hurdle Challenge 2, does it still work? How could we improve the readabillity of our code?