Page - 33 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 33 -
Text of the Page - 33 -
1.7 ErrorMessagesandWarnings 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?
Hewould typehis age,pressenter, and thecodewouldproceed likebefore.
Thereareotherwaysofprovidinginput toaprogramaswell,e.g.,viaagraphical
interface(asmanyreaderswillbeusedto)orat thecommandline(i.e.,asparameters
succeeding, on the same line, the command that starts the program). Reading data
fromafile isyet anotherway.
1.7 ErrorMessagesandWarnings
Allprogrammersexperienceerrormessages,andusually toa largeextentduringthe
early learning process. Sometimes error messages are understandable, sometimes
theyarenot.Anyway, it is important togetused to them.
Oneidea is tostartwithaprogramthat initially isworking,andthendeliberately
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
orwarning later.
Debugging Veryoften,youwillexperiencethat thereareerrors in theprogramyou
havewritten. 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.Thereare differentwaysofdoingit too.
A special program(debugger)may be used to help youcheck (anddo) 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.Firstofall, this issomethingyoushoulddo(several times)duringprogram
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 removedand repeat the processwith the
new code. Using simple numbers and doing this in parallel with hand calculations
ona pieceofpaper (forcomparison) isoftena verygood 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
programasks theuser togiveanumber, thingsmaygoverywrongif theuser inputs
the word five in stead of the number 5. In Python, such cases may be handled
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik