Comp sci python
Chapter 5 Lesson 1
What are some example syntax errors?
Missing quotation marks, missing parentheses, incorrect indentation, missing a colon after anifstatement, or not following Python's syntax rules.What kinds of information will you find in syntax error messages?
The file name, line number, the line of code containing the error, a caret (^) pointing near the problem, and a description of the syntax error.What are some example runtime errors?
Using the wrong variable, choosing the wrong comparison operator, writing statements in the wrong order, or producing incorrect program results even though the code runs.Do you receive any Python error messages for runtime errors? Why or why not?
No. Runtime errors produce incorrect behavior or output, but the code is still valid Python, so no error message is generated.What is an exception and when are they thrown?
An exception is a runtime error that completely stops the program. It is thrown when the program attempts an invalid operation.What kinds of information will you find in runtime exception messages?
The file name, line number where the exception occurred, the line of code that caused it, and the type of exception.What are some example runtime exceptions?
Division by zero, which produces aZeroDivisionError, and attempting other invalid operations that Python cannot execute.
Chapter 5 Lesson 2
What information should you gather prior to starting a code review?
Understand the runtime error you are trying to fix and gather clues from incorrect output or exception messages.What are some common questions you would ask as you examine each statement in a code review?
What is the statement supposed to do?
Is it written correctly?
Is it formatted clearly?
What results should it produce?
Do the comments match the code?
What variables does it use?
Could the data be incorrect?
Could this statement be causing the runtime error?
Do comments provide useful information when conducting a code review?
Yes. Comments can provide important clues about what the code is supposed to do, although they can sometimes be incorrect.Do you always need to start a code review at the beginning? Why or why not?
No. You can start in a suspicious area of the program if you are confident earlier code is working correctly.What Python statement do you add to insert program traces in your code?
Theprint()statement.What kind of information might you display in trace statements?
Variable values, calculation results, and messages showing whichif,elif, orelseblocks are running.Are trace statements intended for the programmer or the end user? Do you leave trace statements in your code when done?
They are intended for the programmer. When debugging is complete, they should be removed or commented out.What kind of software tool will let you examine a program and variable contents as the program runs, step-by-step?
A debugger.
Chapter 5 Lesson 3
What are the two main states a program can be in?
The two main states are running and break.When in the running state, can you observe each line of code as it runs?
No. The program runs too quickly to closely observe each line.How do you load the Python debugger for a program?
Addimport pdbat the top of the program.How do you set a breakpoint in Python? Where can your breakpoint statements go?
Usepdb.set_trace(). Breakpoints can be placed anywhere a normal statement can go, including insideif,elif, orelseblocks.What information do you see at the (Pdb) prompt when a program hits a breakpoint?
You see the file name, line number, the statement about to execute, and the(Pdb)command prompt.What are some basic pdb commands?
How do you step to the next line?
Usestepors.How do you run to completion or the next breakpoint?
Usecontinueorc.How do you display variable contents?
Usep <expression>.How do you show a list of nearby code?
Uselistorl.How do you quit the program immediately?
Usequitorq.How do you repeat the last command?
Press Enter or the space bar.Where can you go to find more information on the Python debugger?
The Python debugger documentation: https://docs.python.org/3/library/pdb.html