10: Logic Errors in Python

There are three types of errors that you’ll come across when learning to program in python (or any language): syntax errors, runtime errors and logic errors.

Find and fix common errors in your code
Logic errors in python: Find and fix common errors in your code

Logic errors are often the hardest to find and fix so the aim of this activity is to help you recognize them, debug them and avoid making them wherever possible.


Page 1: Intro

Page 2: The theory: learn what you need to know as fast as possible.

Page 3: Try it: try out and adapt some working python code snippets.

Page 4: Debug it: Learn how to find and fix common mistakes.

Page 5: Extend it: Choose a project idea to use your newfound python skills.

Create.withcode.uk now supports debugging python code with breakpoints

I made create.withcode.uk a few years ago to try to make it as easy as possible for my computing students to interact with python code examples on any device. I wanted to be able to step through lines of code, discuss how it worked then share it with students in a way that let them tweak it, debug it, extend it and share it without the hassle of creating user accounts.

I’ve been thinking a lot recently about how to boost resilience and independence for students in a Computing classroom, particularly when it comes to debugging.

One thing that I’ve noticed that makes a big difference to students is if they’re taught early on how to use debugging tools like breakpoints, stepping and watches.

Step: running one line at a time to check the sequence of instructions runs in the order you’re expecting

Watch: checking the value of a variable as each line of code executes to see how data changes

Breakpoint: telling your code to pause when it gets to a particular line of code so that you can see what happens next

Useful debugging features of an Integrated Development Environment

Stepping and watching variable values has been possible for a while in create.withcode.uk but yesterday I released an update to allow you to set breakpoints on any line of code.

Debugging python code with breakpoints with create.withcode.uk
Debugging python code with breakpoints with create.withcode.uk

The above example shows some python code which will draw some butterfly in random colours.

On the right of the screen you can see the value of each global and local variable. This is really useful for explaining how data changes as your code runs.

The red blobs next to the line numbers show two breakpoints on line 21 and 28. You can toggle a breakpoint on or off by clicking on the line number.

To run the whole code (or up to a breakpoint) in create.withcode.uk, press Ctrl + Enter

To step through one line of code at a time, press Ctrl + .

To run the whole code (up to a breakpoint or the end of the program) press


To step through just one line of code press

Ctrl+Full Stop

instead. Try out the breakpoints in the example below:

It’s a work in progress, so let me know when you find any bugs or if you have any suggestions on how to improve it.

Happy debugging!

Free iMedia R081 Revision Games

I’ve got some fabulous iMedia students in my classes this year. Some of them are really keen to pick up and apply practical skills but struggle in an exam to understand and use the right key words.

I’ve been meaning to put together a selection of game based learning activities for ages and so I thought it’d be useful to try to make some that students could pick up and play with a spare 5 minutes, either at the start of a lesson whilst I’m checking homework or whilst they’re on the bus or wherever.

So, this is a work in progress, but I think it’s ready to be shared: tools.withcode.uk/keywords

Free iMedia R081 revision games

There are 18 learning objectives, split into 4 topics for the R081 Pre Production Skills examined unit. Spread across those learning objectives are over 150 pairs of keywords with matching definitions.

The plan is to have a load of micro revision games that you can use to learn or test yourself. So far I’ve made 4:

Snake: press W, A, S or D to direct the snake to eat each letter of a keyword when you’re given its definition

Eliminate: Rule out all of the keywords that don’t match a definition until you’re left with the right one.

Hangman keyword: Guess letters of a keyword when you’re given its definition.
Hangman definition: Guess letters of a definition when you’re given a keyword.

Alternatively, you can choose any game at random

Randomly play any of the above games

You’ll notice each of the games displays the keywords or definitions in a funny looking font. This is a bit of an experiment based on some research at RMIT University in Melbourne Australia on how to make your brain engage more to remember what you read.

Some of my students find this useful and some find the font really annoying. The jury’s out on that one, so you can turn it off on the About page if you find the text too hard to read!

I hope it’s useful. Let me know if you have any suggestions or spot any mistakes.