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:
a = 40
b = 50
c = 60
avg = a + b + c / 3
print('Average =', avg)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.
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:
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.
length = 5
breadth = 3
area = length + breadth
print('Area =', area)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.
p = 10000
r = 7.5
t = 3
si = p * r * t
print('Simple interest =', si)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.
x = 10
y = 20
x = y
y = x
print('x =', x, 'and y =', y)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:
Work one example out on paper before you run it. If you do not know what 50 should be, you cannot notice 110.
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 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:
5All three kinds, side by side
| Syntax | Run-time | Logical | |
|---|---|---|---|
| Python notices | Before running | While running | Never |
| Error message | Yes | Yes | None |
| Program runs | Not at all | Partly | Fully |
| Answer | No answer | No answer | Wrong answer |
| Example | print('Hi' | 10 / 0 | a + b + c / 3 |
| Who finds it | Python | Python | You |
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?
price = 250
print('Cost is', priceSyntaxError: '(' was never closedWhich kind of error is this?
7Recap
A program runs, shows no error message, and prints an answer you know is wrong. Which error is it?
Why can Python not find logical errors for you?
Which of these is the best way to find a logical error?