7_2_5

WHAT: Use logical reasoning to detect and correct errors in algorithms

HOW:

Activity 1 - Read this

Logical reasoning is stepping through a problem and spotting the conditions or rules within them to see how it is solved. We can use logical reasoning to find errors in our algorithms. We can look at an algorithm and say "this SHOULD happen like this". However, when we carry out the algorithm, it sometimes doesn't work in the way that we had planned.

By taking a step back from our algorithm, we can use our logical brains to find out where the algorithm went wrong.

Activity 2 - Watch this video

Activity 3

The video shows a student trying to get a "Teacher Robot" to make a jam sandwich. If you watch it carefully you will see that the teacher finds it tricky to act like a robot and sometimes uses his human brain to anticipate the meaning of the instruction. This is normal human behaviour but computers can't do that. They need precise instructions (unless they are using a deep learning algorithm, which is slightly different).

Try and write your own instructions for the teacher robot. Get a partner to act them out so that you can try and spot any errors in your algorithms.

Activity 4 (Watch and Read)

The video above shows a typical error that is made when using Scratch to program for the first time.

This was the original algorithm:

  • When right arrow pressed
  • Move right 10 steps
  • When left arrow pressed
  • Move left 10 steps

The command used each time is "move 10 steps". The computer doesn't know that it wants to move left when the left arrow is pressed so we must give the program that instruction.

We can use our logical reasoning skills to solve the problem. By looking at the other motion blocks you can see that we can change the x position instead of moving 10 steps. Using this, fixes the error.

Activity 5 (Watch and Read)

Another error has been made with this python code. The original algorithm was:

  • Say hello
  • Ask the user how they are
  • if they answer "good" then say "That's nice"
  • else say "Oh dear"

When we step this through to try and spot the error, we notice that the spelling of GOOD and good are different. Python won't change the case of words without being told to. There are two ways to solve this problem:

  • 1 - The method shown in the clip
  • 2 - using .upper() at the end of the input statement

CHECK:

EMBED:

Activity Option 1:

If you are familiar with Python then copy and paste this code into a new file, try to find all five errors and then fix them.

answer=input("Type your name here :"
if anwser=="ben"
  print "Hi BEN!")
  peint ("It is a lovely day today")

Activity Option 2:

Download this file, open it with Scratch and fix the code so that the ball goes around the maze.

If this doesn't work for some reason then the original code and maze image are here:

CLASSROOM IDEAS:

Just like with 7_2_4, the students need lots of practice fixing errors. You could try the "Teacher Bot" activity yourself to get them thinking about precise instructions and correcting errors.