How to Handle Python Programming Problems - dummies

How to Handle Python Programming Problems

By Brendan Scott

When Python runs into an error, it tries to give you a clue about what went wrong and where. Try to figure out Python’s message. It will usually give you a good place to start.

There are two main kinds of errors in Python:

  • A syntax error basically means you made a punctuation mistake. These are the most common errors. Get used to reading what you’ve typed very closely and comparing it carefully to what you should have typed.

    If you have trouble getting one of the code examples to run properly, the first thing to do is make sure that you’ve retyped the code exactly as it’s written.

  • Logic errors occur where you’ve misunderstood what you’re trying to do or what you’ve asked Python to do. These errors can be difficult to spot and more difficult to track down. Compare the output that you actually get to the output you ought to get. This means that you need some way of working out the output that you ought to be receiving.

Sometimes you get an error because you’ve assumed something that is incorrect. If you can’t see what’s wrong, think about the assumptions you’ve made. If you’re not making any progress, do something else and reset your brain. Go drink some water, feed Fluffy, or stand up and walk around. Do something that gets your mind on something else, but doesn’t steal your brain. (Hopping onto Instagram isn’t a good idea, for example.) When you’ve finished that, come back to your Python problem. Fresh eyes and a fresh mind will help you see the problem differently and hopefully lead to a solution.

If all else fails, bring Fluffy (or your dog, or goldfish, or your pet rock) nearby when you’re coding. If you come to a problem you can’t solve, stop what you’re doing and explain the problem to your pet or object.

Explaining things out loud is an old debugging (error fixing) technique. It works because, in order to explain the problem to someone else, you have to first understand it yourself.

You need to express your problem using words because this causes a different part of your brain to kick in and you think differently about the problem. If you’re not the talking type, keep a programming journal. The concept is still the same — write out an explanation of the problem in your journal and why you can’t crack it. This will help you solve many difficult problems.