When it goes wrong
It is very common to have errors in our programs, or so-called bugs. It affects even experienced programmers!
Some bugs occur because we make mistakes when writing code. As we get more used to writing Python it's more common with code that works but doesn't do what it's supposed to do. Computers are very literal, they do what they're told to do, even if it doesn't make any sense.
Luckily, the computer also tells us what went wrong. We will learn to interpret that here.
Syntax errors
Look att the following code:
favorite_number = int(input("What is your favorite number?"
double = favrite_number * 2
print(double)
The code doesn't run, even if at first glance it looks correct. It's wrong in two places - can you see where?
(There are two right parentheses missing at the end of line 1, and favorite is misspelled on line 2)
When the computer cannot interpret what we've written, often because we're missing a parenthesis or quotation mark, we call it a syntax error. We've violated the grammar of the Python language.
Execution error
Here is another example:
width = input("Width: ")
height = input("Height: ")
area = width * height
print(f"The area of the rectangle is {area}")
Can you see what's wrong? This is slightly trickier.
(The code itself is correctly written - there are no syntax errors - but the variables width and height are never converted to int. Because they are strings, they cannot be multiplied on line 3. The program then crashes on line 3.)
If you try to save the same code to a file called test.py and run it in IDLE, it can give something like this:
Width: 12 Height: 23 Traceback (most recent call last): File "test.py", line 3, in <module> area = width * height TypeError: can't multiply sequence by non-int of type 'str' |
OBS! Felmeddelandet är på engelska eftersom IDLE är programmerade i engelska!
This is another type of error - an execution error. They can be difficult to detect before they actually occur.
Preferably, we read the error message from the bottom up. Look at the last row:
TypeError: can't multiply sequence by non-int of type 'str' |
This tells us what happened - we can't multiply strings (str). The two lines above display what code we tried to execute:
File "test.py", line 3, in <module> area = bredd * höjd |
It's in the file "test.py" on line 3 that this error occured. It doesn't neccessarily mean that the error is there, but that's where Python couldn't figure out how to continue. we can fix the program by converting to int on lines 1 and 2, like this:
width = int(input("Width: "))
height = int(input("Height: "))
area = width * height
print(f"The area of the rectangle is {area}")
Logic error
A last type of error that can occur is called logic error. There are situations when we've written proper code according to the rules of Python, and the program doesn't crash, but still doesn't do what we want it to. Here is an example:
number = int(input("Enter your favorite_number: "))
double = number * 10
print(f"Double your favorite number is {double}")
Do you see the error? Can you fix it?
(We must multiply by 2, not by 10, on the second line.)
In this video, we look at a code example that has a syntax error, execution error, and logic error, and then solve all these bugs.
- Bug: An error or mistake in a computer program.
- Syntax error: When the computer cannot interpret what we've written, often because we're missing a parenthesis or quotation mark, we call it a syntax error. We've violated the grammar of the Python language.
- Execution error: The code is grammatically correct but tries to do something impossible and crashes. For example, converting the string "abc" to an int - it's not possible. We get an error message instead.
- Logic error: The code is grammatically correct, the program does not crash, but it doesn't do what we really wanted it to. For example, we wrote a plus sign instead of a multiplication sign.
- Integer: A whole number, without decimals, like \(5\) or \(42\). Shortened to "int".