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.4a

LO: To solve problems using conditional statements. (1)

We are going to look at two worlds/problems to solve using the conditional statements while and if. The first world is called around home:

To solve this world, Reeborg has to walk around the path (anticlockwise) and finish at the goal/home. We will start with our while loop:


while not at_goal():
    #do what you need to do to complete the world/problem
else:
    done()

			

The while loop deals with the overall problem - Reeborg has to finish at the goal/home. Have another look at the conditions that Reeborg is aware of, can you think of an if statement that will help Reeborg walk around the path and not crash into any walls?

Have a look at this flow chart for an if statement:

Have a go at writing the code for this problem, test it and debug it if necessary.*



Some advice:


#you will need to move forward one space!!!
move()
while not at_goal():
    #do what you need to do to complete the world/problem
else:
    done()

			

Now let's look at the same problem, but from the other way around with the world around home 2.

Reeborg has to make his way around the path, clockwise this time, until it reaches the goal/home. Way back in 3.1a when we first looked at writing functions the first example was how to teach Reeborg to turn right - that might come in handy here...

Have a go at writing the code for this problem, test it and debug it if necessary.*



Some advice: define the function to turn right outside of the main while loop, you can call the function from inside the if statement.





* Solutions can be found in the code examples below.

Code Examples

Resources