-
Notifications
You must be signed in to change notification settings - Fork 7
Session 2: Tools II
- Time: 2h
- Date: Tuesday, Feb-4th-2020
-
Goals:
- Learn how to execute programs step by step
- Learn the difference between step over, step into and step out
- Practice with all the concepts introduced so far
- Introduction
- Step by Step execution
- Stepping into a function
- Exercises
- End of the session
- Author
- Credits
- License
Writing programs is not difficult. What is more complex is making them work as expected. When our programs do not work or generate strange errors, we say they have bugs. The process of finding the errors is called debuging: we want to remove all the bugs
In this session we will learn how to debug our programs by executing them step by step
The Pycharm IDE has an integrated tool for letting us to execute our programs line by line. This is what is called as step by step execution
Let's practice with the hello.py program that we already did in the session 1. It should be in the Hello folder in our local working repository (2019-2020-PNE-Practices)
As we have already execute it one time, there is an existing configuration. Therefore, we can execute again very easily just by clicking on the green play button (make sure that the selected configuration is hello)
A new Run panel is opened below. We can see there the output of the print instructions
As we are running the program, all the prints are executed one after another until the program finished. What we want to do now is executing them step by step
The first step is to select the line in which we want the program to stop. This is called a breakpoint. The program is executed until the breakpoint is reached. Then, the program is paused, and we can examine what has happend in the output console
Let's place a Breakpoint in the first print instruction. Go to the right side of the line number and doble click with the mouse. A new red circle will appear
We have placed a Breakpoint in the line number 1
once the Breakpoints are set (we can place more than one if we want) we press the green bug button on the right side of the run button
The program begins to execute until the first breakpoint is reached. In our example, as the breakpoint is on the first line, none instruction is executed. The instruction on the breakpoint is highlighted
A new debug panel opens in the bottom. The debugger tab is automatically selected. we can also notice a green point on the bug. This is for indicating that currently we are in debug mode
Click on the Console Tab right to the debugger tab
There is were the printed messages will appear
Let's execute the current instruction. Press on the small button that says STEP OVER (or press the F8 key)
We can see on the console the message printed by the first instruction. The line with the next instruction is highlighted
Congrats!! You have executed your first instruction in debug mode!
We repeat the process for executing the other two remaining instructions: We click on the STEP OVER button again and the next message is shown in the debug console
Finally, we click on STEP OVER again and the last instruction is executed and the program finished
The debug mode is finished. We no longer see the green dot under the bug
In this animation all the process is shown
Let's debug our second program. It is the exercise number 2, from the session 1. It prints the numbers from 1 to 20. This is the code:
# Session 1. Exercise 2
for i in range(1, 21):
print(i, end=' ')
Let's place a breakpoint in the print statement, in line 4. Chose the count configuration (or run the program if there is not a configuration yet) and press the debug button
The program is executed. It enters the for loop and reach the Breakpoint. Notice that inside the loop there is one variable i. Its value can be seen on the lower panel in the right. Currently i = 1
The i variable can also be seen on the right of the for statement ( i: 1). Click on the Console TAB and STEP OVER the print instruction
The next instruction is highlighted (for) and the value of the i variable is printed on the console. STEP OVER again
The variable i is incremented to 2, and the next print instruction is highlighted
Now, instead of Stepping over, let's resume the program clicking on the resume button (on the left) or by pressing the F9 key:
The program is executed until the next breakpoint is reached:
We press the resume button again. We reach the same line, but the variable i has been incremented
We repeat this process until we reach the last value
The variable i is 20 (but it has not been printed yet on the console). We press the resume button for the last time. The execution is completed and the debug mode ended
It is easy to debug the programs, right? 🙂
In this animation the above execution can be seen
Let's debug the exercise 3 from the session 1: summing the first 20 integer numbers. This is the code:
# Session 1. Exercise 3
# Calculate the sum of the 20 first integer numbers
# 1 + 2 + 3 + ... + 20
# Example working!
# -- Store the result
res = 0
for i in range(1, 21):
res += i
print("Total sum: ", res)
Let's place the Breakpoint on the line number 10 (on the res += i sentence), chose the sum20 configuration and click on the debug button
Now there are two integer variables: i and res. We see them in the Variables tab and in the code window. We press the resume button several time to see how the res variable is changed
We repeat the process. Then the variable i is 20 (last value) this is what we see:
we press the resume button for the last time and see the result on the console. The debug is finished
Let's create a new program for calculating the sum of the 20 first integers as well as the 100 first integers. We will call it as sumN.py
As this is a new program created during the session 2, we will store it in a new folder called Session-02
We need to convert the sum20.py program into a function, and then called it twice, using the arguments n=20 and n=100. The code is this:
# Function for calculating the sum of the
# N first integer numbers
def sumn(n):
res = 0
for i in range(1, n+1):
res += i
return res
# -- The main program starts here
print("Sum of the 20 first integers: ", sumn(20))
print("Sum of the 100, frist integers: ", sumn(100))
Before executing step by step, let's run it
We see the results on the console. A new configuration called sumN has also been created
Place a Breakpoint in the first print sentence and execute debug
If we press the STEP OVER button, the sumn function is called, the result is calculated, printed on the console and the next print instruction is highlighted. Notice that we have not executed the instructions of the sumn function step by step
If we press the STEP OVER button again, the same happens: the new value is calculated by calling to the sumn function, and the result value is printed on the console. Then the program is finished
Let's debug again. Click on the debug button. Now, click on the Step into button
You can see that the next instruction highlighted is now the first inside the sumn function. Instead of moving to the next instruction in the main program, it has stopped in the first instruction inside the function
If we press the Step over function, we can see what is going on inside de function, exactly in the same way than we did before. We can also see the variables used in the sumn function
After executing some instruction, this is what we see:
If we want to finish the execution of the sumn function and stop in the upper level, were the function was previously called, we press the STEP out button
After that, the instruction were the sumn function was called is highlated
We press the STEP over function and see the result on the console, just like the previous debugging. The next instruction is highlighted. Notice that now we no longer see the n,res and i variables. We are in an outer scope where that variables do not exists. They only exist in the sumn scope
We can now step over or step into the sumn function. Let's step into again
As we are calling the sumn function with the parameter 100, the n variables equals 100 now (in the previous call it was n = 20)
We can finish the execution by clicking on the resume button. As there are no more Breakpoints, it will complete the execution until the end
The execution explained above can be seen in this animation:
Now it is your turn. The only way of learning how to debug a program, is debugging many programs!
Make sure that you have upload all the exercises form the session 1 (that we have already solved in this session) in Github. The more you practice, the better. Then, continue with the exercises from the session 2:
- Filename: Session-02/fibonacci.py
- Description: Write a program (without creating any function) that prints on the console the first 11 terms of the Fibonacci series (staring from 0)
- Execute the program step by step
- The output should be like this:
- Filename: Session-02/fiboN.py
- Description: Convert the previous program into the function fibon(n) that calculates the nth Fibonacci term and return it
- The main program should call the fibon() function and print the 5th, 10th and 15th terms in the console
- Execute the program step by step
- Use the step into button for watching how the fibon() function works. Try also the step out button
- The output should be like this:
- Filename: Session-02/fibo-sumN.py
- Description: Write a function called fibosum(n) that calculates the sum of the n first fibonacci terms. The main program should call this function twices, with the arguments n=5 and n=10
- Execute the program step by step
- Use the step into and step out buttons for debugging. Notice that now you have 2 functions at different levels (one function is calling the other). Use step into twice to enter into the fibon() function
- The output should be like this:
The session is finished. Make sure, during this week, that everything in this list is checked!
- You have all the items of the session 1 checked!
- Your working repo contains the Session-01 Folder with the following files:
- count.py
- sum20.py
- Your working repo contains the Session-02 Folder with the following files:
- sumN.py
- fibonacci.py (Exercise 1)
- fiboN.py (Exercise 2)
- fibo-sumN.py (Exercise 3)
- Juan González-Gómez (Obijuan)
- Alvaro del Castillo. He designed and created the original content of this subject. Thanks a lot :-)
S0: Introduction
S1: Tools I
S2: Tools II
S3: Practicing with the tools
S8: Client-Server-1
S9: Client-Server-2
S10: Client-server-3
S11: Client-server-4
S12: HTTP protocol-1
S13: HTTP protocol-2
S14: HTTP module
S15: HTTP module
S16: HTML forms
S17: HTML forms