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

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

Bild der Seite - 90 -

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

Text der Seite - 90 -

90 4 FunctionsandtheWritingofCode Based on these reflections, we choose to break up the programming task into threesteps: • 1stversion—hasnodialoguewith theuser. It contains thedouble loopconstruc- tion and two functions, ask_user and points. The function ask_userwill (in later versions) ask the user for an answer to a*b, while points (in later versions) will check that answer, inform the user (correct or not), and give a score (1point if correct).To simplify, the functionbodiesof these twofunctions willdeliberatelynot becodedfor thisversionof theprogram.Rather,wesimply insert a print command in each, essentially printing the arguments provided in thecall, to confirmthat the functioncallsworkasplanned. • 2nd version—asking and checking done systematically with predictable ques- tions (first the1 times table, then the 2 times table, etc.). • 3rd version—asking and checking done with randomized questions. How to implement this randomization,willbe keptasan openquestion till weget there. (We do reveal, however, that something “unforeseen” will be experiencedwith the 3rdversion,whichwill motivateusalso fora 4thversionof the program.) 4.2.2 The1stVersionofOurCode Ourveryfirst versionof thecode (times_tables_1.py)maybewritten like this: def ask_user(a, b): # preliminary """get answer from user: a*b = ?""" print(’{:d}*{:d} = ’.format(a, b)) return a*b def points(a, b, answer_given): # preliminary """Check answer. Correct: 1 point, else 0""" print(’{:d}*{:d} = {:d}’.format(a, b, a*b)) return 1 print(’\n*** Welcome to the times tables test! ***\ \n (To stop: ctrl-c)’) # Ask user for a*b, ... a, b are in [1, N] N = 2 score = 0 for i in range(1, N+1, 1): for j in range(1, N+1, 1): user_answer = ask_user(i, j) score = score + points(i, j, user_answer) print(’Your score is now: {:d}’.format(score)) print(’\nFinished! \nYour final score: {:d} (max: {:d})’\ .format(score, N*N)) In this implementation, the functionask_userwill, by choice, not ask the user about anything. It will simply return the correct answer. Regardingpoints, it will
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