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

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

Image of the Page - 93 -

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

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