Writing reliable code is hard. This is why we need to design & run tests to check that the code does what it is meant to do and then use debugging techniques to find and fix any problems.
There are four types of error we should identify:
In interpreted languages (like Python) there are no compile-time errors, so both compile-time-like and run-time errors will crash the program due to unhandled exceptions.
Although there are static analysis tools that will act like a compiler and help you find errors before you run your code.Back to Unit 4 Programming
See 9.2-9.3 in the textbook
Code should be tested regularly as it's being written - testing different modules as they are written and assembled. A wide range of values and situations should be tested, ensuring that you cover at least the following three categories:
For each test you should know what the expected outcome is BEFORE you run the test. This way you can ensure your program is working as expected. You can document your tests in a test table with the columns
The three central bold columns are required - the other columns are strongly recommended.
If a test fails then the program needs to be fixed to make the test pass. This might require the use of debugging techniques (see below) to identify what/where the error is.
If all tests pass then your code might be correct! Write some more tests to be more sure :)
To use the debugger you need to create one or more break points in your code. When you run your code using the Debug button it will stop at these break points and wait for you to look at the current state of the program and chose what to do next.
Once your code has stopped at a break point you can view all of the variables in your code (some debuggers also let you modify their values and run code in the console). You can then Step Over to run each line of the code line-by-line or Continue to the next break point.
Step-In and Step-Out are used to enter and exit function or method calls.Once you've identified where the code was broken / had the unwanted behaviour, then you can fix it and run your tests again!
The get_grade
function is meant to calculate a letter grade given mark and a list of cut-offs. It has problems... fix them!
def get_grade(mark, ABCD_cutoffs):
"""Returns the letter grade for a mark \
given the cutoffs for A, B, C and D.
A mark below the D boundary receives an F.
"""
GRADES = ['A', 'B', 'C', 'D]
grade == 'F'
for idx in range(4)
if mark >= ABCD_cutoffs[idx]:
grade = GRADES[idx]
return grade
# test grades A B C D
boundaries = [80, 70, 60, 50]
# write your test data
START get_grade(mark, cutoffs)
DECLARE GRADES ← ['A', 'B', 'C', 'D']
....
RETURN grade
END get_grade