A logic error always does the following
- It allows the program to run (or at least to start).
- It leads to unexpected results . The program might seem to work fine 99% of the time, but during that other 1% of the time, something weird occurs.
Example of a Logic Error in Penjee:
As you can see from the above animation, we wrote code, and
- the program did actually start up
- our code lead to unexpected results (because our code’s logic was wrong. We turned the wrong way!)
Now, in many ways, the above example is not representative of many logic errors in production code. This example is so easy to identify . This, of course, is because Penjee is a visual tool for teaching kids Python , so let’s continue exploring logic errors in the larger world of programming.
Typical Traits of Logic Errors
Below is a list of other stuff that’s usually true about logic errors.
- They’re harder to detect than syntax errors . If you make a syntax error in Python by forgetting a colon or messing up an indent, you’ll learn that real quick!
- They arise from special cases in a program, a specific case that you didn’t think about.
- They might crash the program, or might not. A program with a logic error will always ,at least, start correctly, and the logic error might or might not cause the program to crash.
Second Example of a Penjee Logic Error
The following example is more representative of the trickiness frequently involved in identifying a logic error.
Our penguin, Pam, wants to collect all of the fish. By all appearances, seems to do exactly that! Here’s a still shot of the final state of this program:
Looks pretty good, right ! Our goal has been achieved right? Pam grab() ‘ed all of the fish, none are visible, right? So, what’s the problem?
Well, the thing is, she actually did not grab() the final fish, the one that she is sitting on in that still shot. One thing all programmers must get used to is–examining the logger to get used to identify logic errors:
If you still can’t find the logic error in this particular case, that’s ok, because we always let you check your answers if you’re stumped .
A Logic error is just one of the two types of errors in computer programming, and here at Penjee, we want to teach kids more than just Python, we want to teach you how to think like a programmer so, read up on these other types of errors or, even better, start learning to program now .