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

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

Bild der Seite - 93 -

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

Text der Seite - 93 -

4.2 ProgrammingasaStep-WiseStrategy 93 and points. Often, however, it is a good idea to finalize one function at a time, particularlywith largerand/ormorecomplicatedfunctions. The reader should take a moment to reflect on the use of functions in general (ask_user andpointshere). They represent “logical units”, each dedicated to a special task.Structuringcodethisway,maygreatlyeasehumancodeinterpretation, whichin turnmakesdebuggingandfuturecodechangesmuchsimpler.“Seen”from the main program,ask_user, for example, is given the factors ofa*b and returns theanswer.The innerworkingsofask_user isneitherknown,norofanyconcern, to the main program. It just calls the function and waits for the returned value. Moreover, the inner workings of ask_usermay be changed in any way, without affecting the code elsewhere in the program, as long as function input and output stays the same. By setting N = 10 in times_tables_2.py, and confirming that the dialogue runs correctly, we have a program that carries out the required test. However, we haveyetanotherstep tomake,whichmeanswehavetofigureouthowthequestions canbe randomized.Let ussee whatwe canmakeoutof it. 4.2.4 The3rdVersionofOurCode How can we randomize the 100 questions, while keeping to the plan of asking each question only once? Based on our previous version(s) of the code, it would benatural tofirst thinkof somethinglike <loop arrangement with 100 iterations> i = <draw random number from 1 to 10> j = <draw random number from 1 to 10> user_answer = ask_user(i, j) # ...NOT what we want! <check answer, inform user> However, it is immediately realized that some productsa*b then will come several times, whereas others, are likely to not appear at all. Clearly, this is not what we want.Somemorereasoningis required. Onesolution, theonepresentedhere, isbasedon twokeyobservations.Thefirst observation, is that the integers 0 to 99 can be used to uniquely represent each of our100products.We mightdemonstrate thisby the followinglittle pieceofcode: for i in range(0, 100, 1): a = i//10 + 1 # integer division b = i%10 + 1 # modulo print(’i = {:d}, {:d}*{:d} = ’.format(i, a, b)) Whenexecuted,wegeta printout like i = 0 : 1*1 = i = 1 : 1*2 = i = 2 : 1*3 = i = 3 : 1*4 = i = 4 : 1*5 = i = 5 : 1*6 = i = 6 : 1*7 =
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