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 debug simple programs.

Before we look at a debug example there is an example of declaring and using variables that we can have a look at. This comes from the 'Tickets using return with user input' example program that is available in the code examples section below. Here we are looking at a function that does some operations on the user input:

def total_cost(number_of_tickets):
    #remember to cast the input to an int
    total = int(number_of_tickets) * tickets 


The function accepts one argument and this comes from the variable storing data from the input() function. There is a comment reminding us to cast the input to an int (because we know that the input from the input() function is stored as a string). The difference here is that the casting is included in another calculation (finding the total), it is not calculated seperatly - look at this example:

def total_cost(number_of_tickets):
    #remember to cast the input to an int
    num_of_tickets = int(number_of_tickets)
    total = num_of_tickets * tickets 


This code achieves the same outcome, but this time the casting of the string number_of_tickets is done seperatly - it is cast to a new variable num_of_tickets. Both of these work and neither is 'wrong' or 'right'. The first example does look neater and uses less code though...

Anyway, as always there are some example files to use for debugging or you can always make some yourself - very easy if a child makes an error in class, just save the file with a prefix 'Debug' before you help them correct their error. This technique is much better than artificially creating bugs because you know what errors have actually been made - maybe no student has made the error of forgetting to close a bracket...

Code Examples