LambdaLabTM
Computer Science · Class 11 · Errors
ErrorsLogical⏱️ 8 min read

Logical Errors

The program runs. No red message, no traceback, an answer on the screen. And the answer is wrong. This is the third kind of error, the one Python will never help you with — because as far as Python is concerned, nothing went wrong at all.

1Python does what you said, not what you meant

A computer follows instructions exactly. If your instructions are legal Python but the wrong instructions, it carries them out perfectly and hands you a wrong answer with a straight face. That is a logical error.

Here is a program to find the average of three subjects. Every line is correct Python. The answer is nonsense:

average.py
a = 40
b = 50
c = 60

avg = a + b + c / 3
print('Average =', avg)
Output
Average = 110.0

The average of 40, 50 and 60 is 50, not 110. Nothing crashed, so nothing told you. The mistake is the missing brackets: / runs before +, so Python divided only c by 3 and then added it on. What you meant was (a + b + c) / 3.

Key Takeaway
A logical error is a mistake in your thinking, not in your typing. The program runs to the end and gives no error message — it just gives the wrong result.

2Your turn: fix the average

Run it, see 110, then put the brackets in the right place and run it again. You are looking for 50:

average.py

3Three more that catch everyone

Logical errors are usually small: one wrong sign, one missing bracket, one step in the wrong order. Read each of these and find the mistake before you read the note under it.

area.py
length = 5
breadth = 3
area = length + breadth
print('Area =', area)
Output
Area = 8

Area is length times breadth, so this should be length * breadth and the answer should be 15. Python cannot know which sign you meant — + is a perfectly good thing to do to two numbers.

interest.py
p = 10000
r = 7.5
t = 3
si = p * r * t
print('Simple interest =', si)
Output
Simple interest = 225000.0

The formula is (p * r * t) / 100. Forget the / 100 and the program still runs — it just makes you a hundred times richer than the bank intends.

swap.py
x = 10
y = 20

x = y
y = x

print('x =', x, 'and y =', y)
Output
x = 20 and y = 20

This one tries to swap two values and loses one instead. After x = y, the old 10 is gone forever, so y = x just copies 20 back. You need a third box to hold the value while you move it — or Python's own x, y = y, x.

4How to catch a silent error

Since the computer will not tell you, you need habits that tell you instead. These three find almost every logical error in a Class 11 program:

Know the answer first

Work one example out on paper before you run it. If you do not know what 50 should be, you cannot notice 110.

Dry run the program

Walk through it line by line with pen and paper, writing down what each variable holds. The line where paper and program disagree is your bug.

print() the middle steps

Print the values halfway through, not just the final answer. It shows you exactly where the numbers went wrong.

The third one in action. The final answer is wrong, but the extra print() shows you the exact moment it goes wrong:

debug_print.py
Finding and fixing is called debugging
Hunting down a mistake in a program is called debugging, and the mistake itself is a bug. It is a normal part of programming, not a sign you are bad at it — professionals spend more time debugging than writing.

5All three kinds, side by side

One question tells them apart: when does Python notice?
SyntaxRun-timeLogical
Python noticesBefore runningWhile runningNever
Error messageYesYesNone
Program runsNot at allPartlyFully
AnswerNo answerNo answerWrong answer
Exampleprint('Hi'10 / 0a + b + c / 3
Who finds itPythonPythonYou

6Practice: name that error

Six programs, and what happened when each was run. Decide which kind of error each one is. Remember the single question: when did Python notice?

🕵️ Name that error
1 of 6 · score 0
program.py
price = 250
print('Cost is', price
What Python said
SyntaxError: '(' was never closed

Which kind of error is this?

7Recap

Key Takeaway
A logical error is a mistake in the plan: the program is legal Python, runs from start to finish, shows no error message, and gives the wrong answer. Python cannot spot it, so you catch it by knowing the right answer in advance, dry running on paper, and printing the middle steps. Finding and fixing bugs is called debugging.
Quick Check

A program runs, shows no error message, and prints an answer you know is wrong. Which error is it?

Quick Check

Why can Python not find logical errors for you?

Quick Check

Which of these is the best way to find a logical error?