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

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

Bild der Seite - 94 -

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

Text der Seite - 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)
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