Debugging Code

Debugging and Tracing Code
Year 10
1 / 36
suivant
Slide 1: Diapositive
ComputingUpper Secondary (Key Stage 4)GCSE

Cette leçon contient 36 diapositives, avec quiz interactifs, diapositives de texte et 1 vidéo.

time-iconLa durée de la leçon est: 100 min

Éléments de cette leçon

Debugging and Tracing Code
Year 10

Slide 1 - Diapositive

KO: To identify common mistakes when programming in Python
All
To identify the common 'mistakes' made when programming in Python.

Most 
To identify the common 'mistakes' when programming in Python and fix them.

Some 
To be-able to use a trace table, when debugging code in Python.

Slide 2 - Diapositive

What is Debugging? What is it's purpose?

Slide 3 - Question ouverte

What do you think the most common mistakes in programming are?

Slide 4 - Question ouverte

Syntax Errors
These are errors that occur when you violate the rules of the Python language. For example, forgetting to include a colon at the end of an if statement, or not properly indenting the code within a loop or function. The compiler will not run with a Syntax error.

Slide 5 - Diapositive

Debugging Code - Syntax Errors

Slide 6 - Diapositive

First Task - Corrected Code
for i in range(10):
    print(i)

Slide 7 - Diapositive

Second Task - Corrected Code
password = input("Please enter your password:")

if password == "Password123":
  print("Password is correct")
else:
  print("Password is not correct")

Slide 8 - Diapositive

Third Task - Corrected Code
count = 0
while count < 5:
    print("Count is:", count)
    count = count + 1
print("Loop finished")

Slide 9 - Diapositive

Name Errors
These errors occur when you reference a variable or function that has not been defined or assigned a value.

Slide 10 - Diapositive

Debugging Code - Name Errors 

Slide 11 - Diapositive

First Task - Corrected Code
x = 42
print(x)

Slide 12 - Diapositive

Second Task - Corrected Code
my_variable = "Hello, world!"
print(my_variable)

Slide 13 - Diapositive

Third Task - Corrected Code
fruits = ['apple', 'banana', 'orange']
print(fruits[1])

Slide 14 - Diapositive

Type Errors
These occur when you try to perform operations on incompatible data types, such as trying to add an integer and a string.

Slide 15 - Diapositive

Debugging Code - Type Errors

Slide 16 - Diapositive

First Task - Corrected Code
x = "42"
y = 2
print(int(x) + y)

Slide 17 - Diapositive

Second Task - Corrected Code
food = "bar"
print(food)

Slide 18 - Diapositive

Third Task - Corrected Code
num1 = "10"
num2 = 5
sum = int(num1) + num2
print(sum)


Slide 19 - Diapositive

Logical Errors
These occur when your code does not produce the expected output due to incorrect algorithms or decision-making. A program with a logical error can still compile and run without any syntax errors, but the program will not produce the intended output or behaviour.

Slide 20 - Diapositive

Debugging Code - Logical Errors

Slide 21 - Diapositive

First Task - Corrected Code
x = (2 + 3) * 4 # addition needs to be performed first
print(x)

Slide 22 - Diapositive

Second Task - Corrected Code
age = int(input("How old are you?"))

if age >= 18:
    print("You are an adult")
    
else:
  print("You are not an adult")

Slide 23 - Diapositive

Third Task - Corrected Code
x = 10
while x > 0:
    print(x)
    x = x + 1

Slide 24 - Diapositive

Casting (Data Type Conversion)
Casting in programming refers to the process of converting one data type to another. It is also known as type conversion.

Slide 25 - Diapositive

Logical 
Syntax
Infinite loops
Uninitialised variables
Misspelled keywords
Missing or extra punctuation
Incorrect data types
Incorrect function or method calls
Division by zero

Slide 26 - Question de remorquage

What do you think a 'Trace Table is'?

Slide 27 - Question ouverte

What is a Trace Table?
  • A trace table is a tool used in computer programming to trace and visualise the execution of a program. It is a table that shows the values of variables at different stages of the program's execution. 

  • The trace table is used to keep track of the program's control flow and the values of the variables as the program executes line by line.

Slide 28 - Diapositive

What does a Trace Table look like?
  • In a trace table, the columns represent the variables used in the program, and the rows represent the stages of the program's execution. Each cell in the table represents the value of a variable at a particular stage of the program's execution.

Slide 29 - Diapositive

Slide 30 - Vidéo

Example

Slide 31 - Diapositive

Fix this code!
Click here

Think about what we have looked at today!

Slide 33 - Diapositive

Solution
def find_average(numbers):
    total = 0
    for num in numbers:
        total += num
    avg = total / len(numbers)
    return avg

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
average = find_average(numbers)
print("The average is: " + str(average))

Slide 34 - Diapositive

What are the benefits of using a Trace Table?
A
Makes code run faster
B
Makes code harder to read
C
Helps identify errors and improve code understanding
D
Allows for easier code duplication

Slide 35 - Quiz

What is a type error in Python?
A
When the code is not indented correctly.
B
When a variable is not defined.
C
When an operation or function is applied to an object of inappropriate type.
D
When there is a syntax error.

Slide 36 - Quiz