Seite - 69 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 69 -
Text der Seite - 69 -
3.3 Branching(if,elifandelse) 69
Python will proceed immediately by also testing the second condition. However,
this test is superfluous, since we knowbeforehand that it will evaluate toTrue!So,
inthisparticularcase,usingtwoseparateif tests isnotsuitable(generally,however,
separateif testsmaybe justwhatyouneed. It alldependsontheproblemathand).
An if-else Construction For our case, it is much better to use an elsepart, like
this
T = float(input(’What is the water temperature? ’))
if T > 24: # testing condition 1
print(’Great, jump in!’)
else:
print(’Do not swim. Too cold!’)
# First line after if-else construction
WhenthefirstconditionevaluatestoFalse inthiscode,executionproceedsdirectly
with theprint commandin theelsepart,with noextra testing!
To studentswith little programmingexperience, thismayseem like a verysmall
thing to shout about. However, in addition to avoiding an unnecessary test with
the if-else alternative, it also corresponds better to the actual logic: If the first
condition is false, then theotherconditionhas tobe true, andviceversa.Nofurther
checkingisneeded.
Anif-elif-elseConstruction Consideringour“advisorprogram”,wehavetoadmit
it is a bit crude, having only two categories. If the temperature is larger than 24
degrees, we are advised to swim, otherwise not. Some refinement seems to be the
thing.
Let us say we allow some intermediate case, in which our program is less
categoric for temperaturesbetween20 and24degrees, forexample.There is a nice
elif (short forelse if) construction which then applies. Introducing that in our
program(andsaving it asswim_advisor.py), it reads
T = float(input(’What is the water temperature? ’))
if T > 24: # testing condition 1
print(’Great, jump in!’)
elif 20 <= T <= 24: # testing condition 2
print(’Not bad. Put your toe in first!’)
else:
print(’Do not swim. Too cold!’)
# First line after if-elif-else construction
You probably realize what will happen now. For temperatures above 24 and below
20, our “advisor” will respond just like in the previous version (i.e., theif-else
version).However,for intermediate temperatures, thefirstconditionwillevaluate to
False, which implies that the Python interpreter will continue with theelif line.
Here, condition 2 will evaluate toTrue, which means that “Not bad. Put your toe
infirst!” will beprinted.Theelsepart is thenskipped.Asyoumightexpect,more
refinementwouldbestraight forward to includebyuseofmoreelifparts.
Programming as a Step-Wise Process The reader should note that, besides
demonstrating branching, the development of the previous program gave a (very)
simple example of how code may be written by a step-wise approach. Starting
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