1.7 Error Messages and Warnings
33
Thus, if the user name was Paul, for example, he would get this question up on his
screen
Hello Paul! What is your age?
He would type his age, press enter, and the code would proceed like before.
There are other ways of providing input to a program as well, e.g., via a graphical
interface (as many readers will be used to) or at the command line (i.e., as parameters
succeeding, on the same line, the command that starts the program). Reading data
from a file is yet another way.
1.7 Error Messages and Warnings
All programmers experience error messages, and usually to a large extent during the
early learning process. Sometimes error messages are understandable, sometimes
they are not. Anyway, it is important to get used to them.
One idea is to start with a program that initially is working, and then deliberately
introduce errors in it, one by one (but remember to take a copy of the original
working code!). For each error, you try to run the program to see what Python’s
response is. Then you know what the problem is and understand what the error
message is about. This will greatly help you when you get a similar error message
or warning later.
Debugging Very often, you will experience that there are errors in the program you
have written. This is normal, but frustrating in the beginning. You then have to find
the problem, try to fix it, and then run the program again. Typically, you fix one
error just to experience that another error is waiting around the corner. However,
after some time you start to avoid the most common beginner’s errors, and things
run more smoothly. The process of finding and fixing errors, called debugging, is
very important to learn. There are different ways of doing it too.
A special program (debugger) may be used to help you check (and do) different
things in the program you need to fix. A simpler procedure, that often brings
you a long way, is to print information to the screen from different places in the
program. First of all, this is something you should do (several times) during program
development anyway, so that things get checked as you go along. However, if the
final program still ends up with error messages, you may save a copy of it, and do
some testing on the copy. Useful testing may then be to remove, e.g., the latter half
of the program (e.g., by inserting comment signs #), and insert print commands at
clever places to see what is the case. When the first half looks ok, possibly after
some corrections, insert parts of what was removed and repeat the process with the
new code. Using simple numbers and doing this in parallel with hand calculations
on a piece of paper (for comparison) is often a very good idea.
Exception Handling Python also offers means to detect and handle errors by the
program itself! The programmer must then foresee (when writing the code) that
there is a potential for error at some particular point. If, for example, a running
program asks the user to give a number, things may go very wrong if the user inputs
the word five in stead of the number 5. In Python, such cases may be handled
Précédent

- 55/350

Suivant