Page - 89 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 89 -
Text of the Page - 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.
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