ERROR DETECTION

We all make mistakes. These mistakes can come along in many different ways. Perhaps you put in the wrong combination for your lock, put your birth year on a form where it was asking for your postal code, or even forgot to put RAM in the computer you were buidling. Just like in real life, error in programming can take on many forms. Read below to find out more.

Syntax Errors

A syntax error occurs when you mistype part of your code, or attempt to use part of the language in a way that does not work.

Examples of a Syntax Error

forgetting to place the hashtag at the start of a comment
printz()
print)(
myVar = iput()

 

Logic Errors

A logic error is an error in the source code that produces an unexpected result. These errors can include using the wrong formula for calculating a result or even asking for the user’s name but storing it into the wrong variable. For the most part your program will run, it will simply display in an unexpected result.

Examples of a Logic Error

# You want to divide 10 by 2. But you divide 2 by 10
x = 10 / 2
# versus
x = 2 / 10

 

Run-Time Errors

Run-time errors occur when, brace yourself, the program is running! These usually occur because of poor error checking.

Examples of a run-time error

# Your code asks for an integer and the user incorrectly types in a letter
userinput = int(input('Please enter your age: '))

INPUT: a
OUTPUT: CRASH!

 

 

Check Your Understanding

Use the interpreter below in order to solve the following problems.

1. Fix the syntax error in the following program, so that it prints out the sum of all the numbers from 1 to 10. You can change at most one character.

print(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 +)

 

2. Fix the run-time error in the following program, so that it prints out Hello on the first line and Joe on the second line. You can change at most two characters.

print("Hello")
username = Joe
print(username)

3. You are going shopping for meat and milk, but there is tax. You buy $2.00 of milk and $4.00 of meat, and the tax rate is 3%. Print out the total cost of your groceries (you don’t need to print the dollar sign).

meatPrice = 4.00
meatTax = 0.03 * meatPrice
milkPrice = 2.00
milkTax = 0.03 * milkPrice
print(meatTax + meatPrice + milkTax + meatPrice)
print(username)