Debug it with code

Find and fix the errors

The code below should be a game of spin the bottle, where an arrow spins around the screen before settling in one direction, but it’s got three deliberate errors in:

A syntax error is a problem with your code that stops it from running. They are the easiest type of error to find because you’ll get an error message telling you the line number near where python thinks the problem is when you try to run your code.

Clue: Line 3 is a list of all of the images the micro:bit is going to display for spin the bottle. A list can store more than one item of data in order. It uses square brackets to surround all the data, and each individual item should be separated by a comma.

 

A run-time error doesn’t stop your code from running but it causes your program to crash if it happens whilst your code is running. Run time errors often happen if you try to access data that doesn’t exist (e.g. you’re trying to read from a file that doesn’t exist, or you’re trying to read from a variable that hasn’t been set yet.

Clue: In this case, the code is trying to look up the 9th item in a list that only contains 8 items.

 

A logical error is the hardest type of error to find but often the easiest to fix once you’ve found it. It doesn’t stop your code from running and often it doesn’t even make your code crash. A logical error is when your code does exactly what you’ve told it to do, but you’ve told it to do the wrong thing.

Clue: Logical errors often happen when you try to multiply instead of divide, add instead of subtract, use True instead of False or something similar. In this case, the code should keep looping round forever, spinning the arrow then waiting for the arrow. What the code actually does is never loop round at all. Why?

If you’ve had a go at debugging the code and you’re stuck, watch the video below to give you some further help: