Let's divert to an important concept in Software Engineering: Types of Errors. We are going to learn about the three main types of errors: syntax errors, logic errors, and runtime errors. By the end of this lab you will hopefully understand how to identify, track, and fix these errors.
Ensure that you are signed into GitHub
Open the assignment: Project 5 - To Do List
Open lab3.py
Syntax errors occur when the code does not adhere to the language's rules and structure. It is similar to a spelling or grammar mistake.
Imagine trying to read a book where some sentences don't have an end, or words are jumbled up without proper order. Just like you can't understand these sentences, the Python interpreter can't understand code with syntax errors.
The code above will produce a syntax error because it lacks a colon at the end of the if statement.
The code above will produce a syntax error because the string is not properly closed with quotation marks.
Identifying: You will get an error with the location/line number and a description of the problem. You will also usually see it underlined in the IDE.
Tracking: Read the error message and inspect the indicated line for common syntax issues.
Fixing: Check for common syntax issues like missing parentheses, quotation marks, colons, etc.
Logic errors occur when the code doesn't function as intended, leading to incorrect results.
Think of it like following a recipe for a cake but accidentally using salt instead of sugar. You've followed all the steps correctly (syntax), but the cake won't taste right because of the wrong ingredient (logic).
The code above is an example of a logic error because the formula for area calculation is incorrect; it should be multiplication, not addition.
The code above is an example of a logic error because the range will only print 1 to 4 due to the end value being non-inclusive.
The code above is an example of a logic error because the condition to check for an even number is incorrect; it should use the modulus operator %.
Identifying: Check if the program's output is different from what you expect.
Tracking: Use print statements to display values of variables at different points in the code.
Fixing: Once you have identified where the logic error is occuring, adjust the logic or conditions in the code to achieve the desired outcome.
Step back, examine your code carefully, use print statements to see what is going on throughout your code and adjust / test.
Runtime errors occur during program execution and can cause the program to crash.
Consider it like driving a car and suddenly running out of fuel. The car (program) was working fine, but an unexpected situation (running out of fuel) causes it to stop working unexpectedly during the journey (runtime).
The code above is an example of a runtime error because line 2 tries to access a non-existent index, causing the program to crash.
The code above is an example of a logic error because it tries to divide a number by zero (which is invalid), causing a crash.
The code above is an example of a runtime error because line 2 tries to access a non-existent file, causing the program to crash.
The code above is an example of a runtime error because line 2 tries to divide a string by a number, causing the program to crash.
Identifying: Observe error messages and crashes during execution.
Tracking: Check error messages for the type and location of the error.
Fixing: The good news is we have covered this already! Implement error handling and validate inputs wherever possible.
Complete the exercises found in Lab 3 which cover:
Syntax Errors
Logic Errors
Runtime Errors
Press Ctrl + F5 to run the program or open a terminal and type python lab3.py
You should now know how to identify and fix the three different types of errors encountered when programming.
Let's wrap up this lab by pushing our code to GitHub:
Enter a commit message. E.g. "Lab 3 complete"
Press Commit to main
Press Push origin