Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 109 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 109 -

Bild der Seite - 109 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 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.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python