Debugger#
You have had an introduction to using the debugger in the Laby exercises. We
will now look at more concrete and useful applications in programming.
To review how to activate it, add a breakpoint, and execute a program step by
step, refer to the Laby sheet.
Observing variables#
Activate the debugger, add a breakpoint on the first line of the following cell
, and execute it:
n = 5
n += 1
n = 10
a = 5
With the debugger, it is possible to follow the evolution of your variables: we will
focus on the “VARIABLES” area of the palette on the right, above the “CALL
STACK”. If it is not expanded, click on the ► triangle to the left of “VARIABLES”.
Execute the cell step by step with the F10
key, and observe the evolution of the values
of the variables in the “VARIABLES” area.
At the end of the execution, you should obtain a: 5
and n: 10
.
⚠️ You will notice that the value of the variables is that of the previous* line*: the
debugger stops on the line before its execution. ⚠️
For example, when the n += 1
line is highlighted, n
still has the value \(5\), and
the instruction n+=1
will be executed when you move to the next step.
If you execute the cell step by step a second time, the variables a
and n
will be
present in the list from the first line: a variable remains stored in
the Python kernel environment until the kernel is restarted, and it contains
the last value you assigned to it.
Control flow verification#
By following a step-by-step program, you can check if your control flow
(the order in which a program’s instructions are executed) works as
expected.
Test the debugger on this for
loop by setting a breakpoint on the first
line.
for i in range(5):
print(i)
0
1
2
3
4
The following function returns the minimum between two numbers a
and b
. Test it for
different values of a
and b
, and observe the execution of instructions using
the debugger. Warning: the breakpoint must be set on the first line after
the function declaration (here, the second line of the cell) for the inside
of the function to be executed step by step.
def minimum(a, b):
if ( a < b ):
return a
else:
return b
minimum(4, 2)
2
Exercises#
Guess the programmer’s intention when writing the following code; then step through it using the debugger to determine where the error is, and correct it.
x = 5
y = 4
résultat = ""
if ( x >= y ):
if ( x == y ):
résultat = "égalité"
else:
résultat = "x est plus petit que y";
résultat
'x est plus petit que y'
### BEGIN SOLUTION
x = 5
y = 4
resultat = ""
if ( x >= y ):
if ( x == y ):
resultat = "égalité"
else:
resultat = "x est plus petit que y";
resultat
### END SOLUTION
'x est plus petit que y'
The following function is intended to take an integer
n
as input and return the sum of all even integers up to and includingn
. For example, forn=10
, it should return \(30\) \((2+4+6+8+10)\). Use the debugger to observe the variables and correct the code. Verify that the tests pass.
def somme_entiers_pairs(n):
i = 2
somme = 0
while i < n:
if i % 2 == 0:
somme = somme + n
i = i + 1
return somme
somme_entiers_pairs(10)
40
### BEGIN SOLUTION
def somme_entiers_pairs(n):
i = 2
somme = 0
while i <= n:
if i % 2 == 0:
somme = somme + i
i = i + 1
return somme
somme_entiers_pairs(9)
### END SOLUTION
20
assert somme_entiers_pairs(10) == 30
assert somme_entiers_pairs(9) == 20
assert somme_entiers_pairs(1) == 0
Summary#
The debugger helps to understand code – and especially to locate errors – by
allowing you to execute it step by step while observing the value of the variables.
For the future, you are encouraged to use the debugger as much as possible to
follow the execution of your programs, and thus avoid excessive reliance on
print
printing instructions scattered throughout your code.