Code Bytes · "Intro to Programming" · Episode 17 · Debugging
Syntax vs logic errors
A syntax error breaks the writing rules of code, so Python won't run it at all. A logic error runs fine but gives the wrong answer, so you only catch it by checking the output.
Syntax: the writing rules of code
Every language has rules for how you write it. In English, "Hello how are you" is missing a comma and a question mark: "Hello, how are you?" Code has rules like that too, and they're called syntax.
A syntax error is code that breaks those rules. Here the closing bracket is missing:
``python print("Hello" ``
Save it as hello.py, run it, and Python prints:
`` File "hello.py", line 1 print("Hello" ^ SyntaxError: '(' was never closed ``
(Your computer prints the whole folder path in front of hello.py; it's shortened here.) The bottom line says what's wrong: the open bracket ( was never closed. Add the ) and the program prints Hello.
A syntax error stops everything
Python reads your whole file before it runs any of it. If the file breaks the rules, Python can't understand it, so it runs nothing, not even the lines above the mistake:
``python print("Hi") print("Hello" ``
This prints no Hi at all, just the SyntaxError for line 2. That's annoying, but it's also helpful: you can't miss a syntax error.
Logic errors: runs fine, wrong answer
A logic error is sneakier. The code follows every rule, so it runs, but it doesn't do what you meant. We want the average of 4 and 6, which is 5:
``python a = 4 b = 6 print(a + b / 2) ``
`` 7.0 ``
No error message, just the wrong answer. Why? Python follows the usual maths order of operations: division and multiplication come before addition and subtraction. So it works out b / 2 first (6 / 2 = 3), then adds a (4 + 3 = 7).
Brackets change the order. Put them around a + b so the adding happens first:
``python a = 4 b = 6 print((a + b) / 2) ``
`` 5.0 ``
Now it's 4 + 6 = 10, then 10 / 2 = 5. In Python 3, dividing with / always gives a decimal number (a float), which is why you see 5.0 and not 5.
Syntax errors stop you, logic errors hide
| | Syntax error | Logic error | |---|---|---| | Does the code run? | No | Yes | | Error message? | Yes, a SyntaxError | None | | How you find it | Python tells you | Check the output against what you expected |
That last row is the habit to build: before you run code, know roughly what answer you expect, then check that you got it.
Try it yourself
Open any free online Python editor (search "online Python") and run:
``python print(2 + 3 4) print((2 + 3) 4) ``
You should see:
`` 14 20 ``
The first line multiplies first (3 4 = 12, then 2 + 12 = 14). The brackets in the second line make Python add first (2 + 3 = 5, then 5 4 = 20). Same numbers, different order, different answer.
Good to know
- Every snippet on this page was run with Python 3 (3.13) and printed exactly what is shown, apart from the shortened file paths.
- A syntax error means the code breaks the language's grammar rules, so Python can't read the file and runs none of it. In a script, not even the lines before the mistake run.
- A logic error is valid code that runs but doesn't do what the programmer meant. Python gives no error message, which is why you have to check the output.
- Python follows the usual maths order: division and multiplication before addition and subtraction, unless brackets say otherwise.
- There's a third kind of error: a runtime error, like the
IndexErrorfrom the last episode. The code is valid, but it fails while running. - Next episode: bits and bytes, how everything on your computer comes down to ones and zeros.
Answer to the comment question
What does this print?
``python x = 2 y = 8 print(x + y / 2) ``
6.0
Python divides before it adds: first y / 2 is 8 / 2 = 4.0, then x + 4.0 is 2 + 4.0 = 6.0. It prints 6.0 (with the .0, because / always gives a float). If you added first and got 5.0, that's the logic-error trap from this episode: to get that, you'd need brackets, print((x + y) / 2).
Test your knowledge
Quick quiz: Syntax vs logic errors
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Python Software Foundation, "Errors and Exceptions": Syntax Errors: syntax errors, also called parsing errors, are reported before the program runs, with the line and a ^ mark
- Python Software Foundation, "Built-in Exceptions": SyntaxError: raised when the parser meets a syntax error
- Python Software Foundation, "Expressions": Operator precedence: / binds more tightly than +, so division happens before addition unless brackets group it
- Python Software Foundation, "An Informal Introduction to Python": Numbers: division / always returns a float
Video transcript
One bug stops your code. The other gives the wrong answer. What's the difference?
A syntax error breaks the writing rules of code, called syntax. It's like a sentence with missing punctuation.
Here, the closing bracket is missing, so Python won't run at all. It says the open bracket was never closed.
A logic error is sneakier. The code runs fine, but it does the wrong thing.
We want the average of four and six: five. We type a plus b, divided by two. Python prints seven point oh.
Why? Python divides before it adds, so that's four plus three. Put brackets around a plus b, and it prints five point oh.
Syntax errors stop you. Logic errors hide.
What does this print? Comment your answer below!
That's your byte.
More from "Intro to Programming"
- "Intro to Programming" EP12Functions
- "Intro to Programming" EP13Parameters and return values
- "Intro to Programming" EP14Lists and arrays
- "Intro to Programming" EP15Indexing starts at zero