Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 109 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 109 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 109 -

Image of the Page - 109 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 109 -

5.2 ExceptionHandling 109 Python has many different exception types, and we use two of them here, KeyboardInterruptandValueError (somemoreexampleswill begivensoon). Unless the user answers with a valid integer, one of these exceptions is raised. A KeyboardInterrupt is raised if we type Ctrl-c to stop execution, whereas a ValueErroris raisedotherwise.Wenotethat ineachcaseanappropriateprintoutis givenfirst.Furthermore,whenaValueError is raised,executionproceedsdirectly with the next question (after the printout).When aKeyboardInterrupt is raised, the printout is succeeded by execution of the break statement. This implies that execution breaks out of thefor loop and the program stops after printing the final score. Onedialoguewith theprogramcould thenbe, forexample: *** Welcome to the times tables test! *** (To stop: ctrl-c) 6 * 8 = 48 Correct! Your score is now: 1 5 * 8 = u (accidentally hit wrong key - author’s comment) You must give a valid number! 3 * 10 = (only press enter - author’s comment) You must give a valid number! 5 * 6 = 30 Correct! Your score is now: 2 7 * 6 = (type ctrl-c - author’s comment) Ok, you want to stop! Finished! Your final score: 2 (max: 100) Withourfinalversion,wesee that sometypicalerror situationsarehandledaccord- ingtoplan,andalso thatCtrl-cnowworksaspreviously.For thepresentproblem, we found that only two different types of exceptions (KeyboardInterrupt and ValueError)were required.Hadmoreexceptionsbeenneeded,wecouldjusthave extendedthestructurestraight forwardly,with except exception_type: <statements> for each of them. Note that it is possible to have a unified response to several exceptions, by just collecting the exception types in a parentheses and separating themwith a comma.For example,with two such exceptions, they wouldappearon the form except (exception_type_1, exception_type_2): <statements> Before ending this chapter on exception handling, it is appropriate to briefly exemplifya fewmoreof themanybuilt-inexceptions inPython.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python