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

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

Image of the Page - 94 -

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

Text of the Page - 94 -

94 4 FunctionsandtheWritingofCode i = 7 : 1*8 = i = 8 : 1*9 = i = 9 : 1*10 = i = 10 : 2*1 = i = 11 : 2*2 = ... ... < longer printout... author’s comment > ... i = 97 : 10*8 = i = 98 : 10*9 = i = 99 : 10*10 = Thus, from the 100 values of i, we can uniquely derive the two factors in all the 100products(!),as theprintoutconfirms.With thesequenceof ivalues just shown, however,weget thesystematicorderingof thequestionsused inour2ndversionof theprogram.So, toget thequestions in randomorder,we needsomethingmore. The second observation, is that the function shuffle (Sect.2.4) from numpy can be used to randomize the numbers 0 to 99, and thereby give us a randomized orderingof the products. Now, based on these two observations, we are ready to write down the 3rd version of our program (times_tables_3.py), in which the functionsask_user andpointsare unchangedcomparedto the 2ndversion: 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 user_answer = ask_user(a, b)
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