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 - 107 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 107 -

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

Text der Seite - 107 -

5.2 ExceptionHandling 107 import numpy as np def ask_user(a, b): """get answer from user: a*b = ?""" question = ’{:d} * {:d} = ’.format(a, b) answer = int(input(question)) return answer def points(a, b, answer_given): """Check answer. Correct: 1 point, else 0""" true_answer = a*b if answer_given == true_answer: print(’Correct!’) return 1 else: print(’Sorry! Correct answer was: {:d}’.format(true_answer)) return 0 print(’\n*** Welcome to the times tables test! ***\ \n (To stop: ctrl-c)’) N = 10 NN = N*N score = 0 index = list(range(0, NN, 1)) np.random.shuffle(index) # randomize order of integers in index for i in range(0, NN, 1): a = index[i]//N + 1 b = index[i]%N + 1 try: user_answer = ask_user(a, b) except: print(’You must give a valid number!’) continue # jump to next loop iteration score = score + points(a, b, user_answer) print(’Your score is now: {:d}’.format(score)) print(’\nFinished! \nYour final score: {:d} (max: {:d})’\ .format(score, N*N)) What will happen here? During execution, Python will first try to execute user_answer = ask_user(a, b) in the try block. If it executes without trouble, the except block is skipped, and execution continues with the line score = score + points(a, b, user_answer). However, if an exception is raised, executionproceeds immediatelywithprintandcontinue in the except block. If so, the assignment to user_answerdoes not take place. Thecontinue statementmakesusmovetothenextquestion,sinceit immediatelybringsexecution tothenextloopiteration,i.e.,score isneitherupdated,norprinted,before“leaving” that iteration. This solution is a step forward for ourprogram,since we avoid an unintentional stopifsomeoneaccidentallyhits thewrongkey.However, theexceptblockherewill handle all kinds of exceptions, and, in particular, trying to stop the program with Ctrl-cwill no longer work (in stead, you may choose Consoles and Restart kernel from theSpydermenu)! It would be better programming to differentiate
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