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 - 90 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 90 -

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

Text of the Page - 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
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