Page - 22 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 22 -
Text of the Page - 22 -
22 1 TheFirstFewSteps
you try to run theprogramto seewhatPython’s response is. Thenyouknowwhat
the problem is and understandwhat the errormessage is about. Thiswill greatly
helpyouwhenyouget a similar errormessageorwarning later.
Very often, youwill experience that there are errors in the program you have
written. This is normal, but frustrating in the beginning. You thenhave tofind the
problem, try tofix it, and then run the programagain. Typically, youfixone error
just to experience that another error is waiting around the corner. However, after
some time you start to avoid themost common beginner’s errors, and things run
more smoothly. Theprocessoffindingandfixing errors, calleddebugging, is very
important to learn. Therearedifferentwaysofdoing it too.
Aspecialprogram(debugger)maybeused tohelpyoucheck (anddo)different
things in the programyouneed tofix. A simpler procedure, that often brings you
a longway, is to print information to the screen fromdifferent places in the pro-
gram. First of all, this is something you shoulddo (several times) duringprogram
development anyway, so that things get checked as yougoalong. However, if the
final programstill endsupwith errormessages, youmaysave a copyof it, anddo
sometestingon thecopy.Useful testingmay thenbe to remove,e.g., the latterhalf
of theprogram(byinsertingcommentsigns#),and insertprintcommandsatclever
places to see what is the case. When the first half looks ok, insert parts ofwhat
was removedand repeat theprocesswith thenewcode.Usingsimplenumbersand
doing this inparallelwithhandcalculationsonapieceofpaper (forcomparison) is
oftenaverygood idea.
Pythonalsooffersmeans todetect andhandleerrorsby theprogramitself! The
programmermust then foresee (whenwriting the code) that there is a potential for
error at someparticular point. If, for example, someuser of the program is asked
(bytherunningprogram)toprovideanumber,andintendstogivethenumber5,but
insteadwrites thewordfive, theprogramcouldrunintotrouble.Atry-exception
constructionmaybeusedbytheprogrammertocheckforsucherrorsandactappro-
priately(seeChapter6.2foranexample),avoidingaprogramcrash. Thisprocedure
of trying an action and then recovering from trouble, if necessary, is referred to as
exceptionhandlingand is themodernwayofdealingwitherrors inaprogram.
When a programfinally runswithout errormessages, it might be tempting to
think thatAh..., I am finished!. Butno! Thencomesprogram testing, youneed to
verify that theprogramdoes thecomputationsasplanned.This is almost anart and
may takemore time than todevelop theprogram,but theprogramisuselessunless
youhavemuch evidence showing that the computations are correct. Also, having
aset of (automatic) tests saveshugeamountsof timewhenyou furtherdevelop the
program.
Verificationversusvalidation
Verification is important, but validation is equally important. It is great if your
programcandothecalculationsaccordingtotheplan,but is it therightplan?Put
otherwise, you need to check that the computations run correctly according to
the formulayouhavechosen/derived. This isverification: doingthe thingsright.
Thereafter, youmust also checkwhether the formula you have chosen/derived
is the right formula for the caseyouare investigating. This is validation: doing
the right things. In the present book, it is beyond scope to question howwell
themathematicalmodelsdescribeagivenphenomenoninnatureorengineering,
back to the
book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik