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

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

Bild der Seite - 89 -

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

Text der Seite - 89 -

4.2 ProgrammingasaStep-WiseStrategy 89 In this chapter,we will use an example to present a typical code writingprocess indetail.Our focuswill beona step-wiseapproach that is sooften required,unless theprogrammeris experiencedand theprogrammingtask is “small”. Often, such a step-wise approach starts out with a very simple version of the final program you have in mind. You test and modify that simple version until it runs likeyouwant.Thenyou includesomemorecode, test andmodify thatversion until itworksfine.Thenyouincludemorecode, testandmodify,andsoon.Eachof thesesteps thenbringsyoucloser toyourfinal targetprogram.Insomecases,all the stepsareclear inadvance,but often,new insightdevelopsalong the way,making it necessary to modify the plan. The step-wise approach is goodalso in that it allows you to get started with a step or two (that you see are needed), even if you do not knowhowto proceedfromthere, at leastyet. How to break up a programming task into a series of steps is not unique. It will also depend on the problem, as well as on the programmer.More experienced programmerscan save time by writing the final versionof some programin onego (or at least with few steps). Beginners, however, may greatly benefit from a step- wise procedurewith the sufficient number of steps. The following example should illustrate the idea. 4.2.1 MakingaTimesTablesTest The Programming Task Write a program that tests the user’s knowledge of the times tables from1to10. Breaking up the Task There are many possible ways to do such a times tables testing, but our reasoning goes as follows. In this test, there will be 10 different questions for the 1 times table, 10 different questions for the 2 times table, and so on,givinga100differentquestionsintotal.Wedecidetoaskeachofthosequestions onetimeonly.Therearequitemanyquestions,sowealsoallowtheuser toquitwith Ctrl-c (i.e.,hold theCtrlkeydownwhile typingc)before reachingthe end. To code this, the first idea that possibly comes to mind, is to use a doublefor loopona formlike: for a in [1, 2, ..., 10]: for b in [1, 2, ..., 10]: < ask user: a*b = ? > < check answer, give points > With a construction like this, we see that for each value of a, the second factorb will runoverall values1 to 10.The questionswill then appear in a predictableand systematic way. First we get the 1 times table (1*1, 1*2, ..., 1*10), then the 2 times table (2*1,2*2, ..., 2*10), and so on. Clearly, this would be an acceptable approach. However, some would still argue that it might be better if the 100 questionswere randomized,deprivingtheuseranybenefit fromjust rememberinga sequenceofanswers.
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