cs 177 week 10 recitation slides 1 1 debugging. announcements 2 2
DESCRIPTION
Roadmap What are computer bugs? When are computer bugs discovered? Compile Time and Runtime What kind of bugs are discovered? Syntax, Arithmetic and Logic Errors Are there tools to help us find bugs? Print statements and Python debugger 3TRANSCRIPT
![Page 1: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/1.jpg)
CS 177 Week 10 Recitation Slides
11
Debugging
![Page 2: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/2.jpg)
Announcements
22
![Page 3: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/3.jpg)
Roadmap
What are computer bugs? When are computer bugs discovered?
Compile Time and Runtime What kind of bugs are discovered?
Syntax, Arithmetic and Logic Errors Are there tools to help us find bugs?
Print statements and Python debugger
3
![Page 4: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/4.jpg)
DebuggingEarly computers used vacuum tubes. The tubes would get hot and attracted moths. A moth was zapped and interfered with the circuitry. The bug had to be removed to fix the computer. Some say this is how the word “debugging” came into use.
4
![Page 5: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/5.jpg)
Debugging
What is a computer bug? A computer bug is a problem that causes a computer to produce
an incorrect or unexpected result.
5
![Page 6: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/6.jpg)
Debugging
Computer bugs can manifest themselves at different phases of the program execution including: Compile Time Runtime
6
![Page 7: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/7.jpg)
When Are Bugs Discovered?
Compile Time, Load Time, & Runtime Bugs
77
![Page 8: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/8.jpg)
Compile Time Bug
Compile time bugs show up when the source code is converted into computer code
A common compile time error is: Syntax Error
A syntax error means that the source code does not meet the source code specification. For example: Missing a ‘:’ at the end of you def statement
8
![Page 9: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/9.jpg)
Compile Time Bug Example>>> def t2()SyntaxError: invalid syntax
9
• Notice the missing ‘:’• When you run this statement, Python
immediately knows that it is invalid code.
![Page 10: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/10.jpg)
Load Time Bug
Load time bugs, in the context of Python, often have to do with the libraries that are imported The permissions may not be set correctly on an imported library
file The library file may not be found
10
![Page 11: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/11.jpg)
Load Time Bug Example>>> import fooImportError: No module named foo
11
• In this case a library named foo does not exist
![Page 12: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/12.jpg)
Runtime Bug
Runtime bugs show up when the computer code is executed
A common runtime error is: NameError
A name error means that a function or variable was used that wasn’t defined
12
![Page 13: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/13.jpg)
Runtime Bug Exampledef t1(): print(a)
>>> t1()
13
• Notice that the variable 'a' is not defined• When you save the file, Python does not report an
error, but when you call the function...
![Page 14: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/14.jpg)
Runtime Bug Exampledef t1(): print(a)
>>> t1()NameError: global name 'a' is not defined
14
• The NameError is produced when the t1 function is called
![Page 15: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/15.jpg)
What are Some Common Bugs?
Syntax, Arithmetic, and Logic Errors
1515
![Page 16: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/16.jpg)
Syntax Bugs
Syntax errors are often discovered by Python at compile time but not always
Likely you have encountered many of these: Incorrect indentation Missing elements (like ':')
16
![Page 17: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/17.jpg)
Syntax Bug
>>> def t1()SyntaxError: invalid syntax
17
• Incorrect syntax
Missing ':'
Python will catch this syntax error
![Page 18: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/18.jpg)
Arithmetic Bugs
We will only focus on one, but a few more exist One important arithmetic bug is a divide-by-zero error
By definition of division, you can't divide by zero
18
![Page 19: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/19.jpg)
Arithmetic Bug
>>> 4/0ZeroDivisionError: int division or modulo by zero
19
• Division by zero:
you can't divide by 0
Python will catch this arithmetic error
![Page 20: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/20.jpg)
Logic Bugs
Logic bugs are usually not caught automatically by the computer like Syntax Errors or Name Errors.
The bug may be subtle and manifest itself in peculiar ways.
Usually takes human source code analysis to track down the bug
20
![Page 21: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/21.jpg)
Logic Bug
>>> i = 0>>> while(i<5):
i = 1
21
• Infinite loop:
i is not gettingincremented
Python will notcatch this
![Page 22: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/22.jpg)
Logic Bug
>>> x = [21,22,23,24]>>> i = 0>>> while i <= len(x):
s = s + x[i] i = i + 1
IndexError: list index out of range
22
• Off-by-one error (or fencepost error):x[4] does not exist
Python catchesthis logic error
![Page 23: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/23.jpg)
Logic Bug
>>> x = [21,22,23,24]>>> i = 1>>> s = 0>>> while i < len(x):
s = s + x[i] i = i + 1
23
• Off-by-one error:
s will not be 90, it will be 69
Python will notcatch this
![Page 24: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/24.jpg)
Are there tools to help us find bugs?
Print Statements and Python Debugger
2424
![Page 25: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/25.jpg)
Print Statements
Strategically places print() statements can be placed in the source code to verify values
Advantage: Using print statements (or equivalents) to debug works in every language, no language specific tool must be learned
Disadvantage: Not everything is printable
25
![Page 26: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/26.jpg)
Using Print Statements
def sort3(x, y, z): print("Input: x=",x,"y=",y,"z=",z) r = sorted([x,y,z]) print("Output:", r)
>>> sort3(8, 11, 3)Input: x= 8 y= 11 z= 3
Output: [3, 8, 11]
26
• Verfiy input and output
![Page 27: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/27.jpg)
Using Print Statements
def t(): s = 0 for i in range(3): ns = s + i print(ns, "=", s, "+", i) s = ns
>>> t()0 = 0 + 01 = 0 + 13 = 1 + 2
27
• Print intermediate live values
![Page 28: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/28.jpg)
Python Debugger
Many programming languages have debuggers available A debugger lets you analyze the program state after
each statement Called stepping
28
![Page 29: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/29.jpg)
Python Debugger
To launch the Python debugger from IDLE: From the IDLE command window choose the menu: Debug-
>Debugger Your command window will show [DEBUG ON] Then run commands as normal and you should see the
debugger window...
29
![Page 30: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/30.jpg)
Using Print Statements
30
![Page 31: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/31.jpg)
Python Debugger
Buttons Go: Run the program normally (don't pause) Step: Run the next statement only then stop Over: Same as step except if the next statement is a function it
will not step into the called function Out: Like go except will allow stepping again once the current
function is complete Quit: Stop run immediately
31
![Page 32: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/32.jpg)
Python Debugger
Options Stack: Current running function Source: Show me in the source what statement is currently
running Locals: What are the values of the local variables Globals: What are the values of global variables
32
![Page 33: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/33.jpg)
Debugger Example
3333
![Page 34: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/34.jpg)
Roadmap
What are computer bugs? When are computer bugs discovered?
Compile Time and Runtime What kind of bugs are discovered?
Syntax, Arithmetic and Logic Errors Are there tools to help us find bugs?
Print statements and Python debugger
CS177 - Spring 2011 3405/03/23
![Page 35: CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2](https://reader036.vdocuments.mx/reader036/viewer/2022081513/5a4d1b1b7f8b9ab0599936b0/html5/thumbnails/35.jpg)
ANY QUESTIONS?
CS177 - Spring 2011 3535