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

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

Image of the Page - 68 -

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

Text of the Page - 68 -

68 3 LoopsandBranching 3.3 Branching(if,elifandelse) Very often in life,5 and in computer programs, the next action depends on the outcome of a question starting with “if”. This gives the possibility of branching intodifferent typesofactiondependingonsomecriterion. As an introduction to branching, let us “build up” a little programthat evaluates awater temperatureprovidedby theprogramuser. 3.3.1 Example:JudgingtheWaterTemperature Assumewewanttowriteaprogramthathelpsusdecide,basedonwater temperature alone(indegreesCelcius),whetherweshouldgoswimmingornot. Oneif-test Asastart, wecodeourprogramsimplyas T = float(input(’What is the water temperature? ’)) if T > 24: print(’Great, jump in!’) # First line after if part Even if youhave never seen anif test before,you are probablyable to guess what will happenwith this code.Pythonwill first ask the user for the water temperature. Let us assume that 25 is entered, so that T becomes 25 through the assignment (note that, since theinput returnsa string, we convert it toa floatbeforeassigning toT).Next, theconditionT > 24willevaluate toTrue,which implies that theprint commandgetsexecutedand“Great, jumpin!”appearson the screen. To the contrary, if 24 (or lower) had been entered, the condition would have evaluated toFalseand the print command would not have been executed. Rather, execution would have proceeded directly to the line after the if part, i.e., to the line # First line after if part, and continued from there. This would mean, however, that our program would give us no response if we entered a water temperatureof24,or lower. Twoif-tests Immediately,werealize that this isnotsatisfactory,so(asa“firstfix”) weextendourcodewitha secondif test, as T = float(input(’What is the water temperature? ’)) if T > 24: # testing condition 1 print(’Great, jump in!’) if T <= 24: # testing condition 2 print(’Do not swim. Too cold!’) # First line after if-if construction This will work, at least in the way that we get a planned printout (“Do not swim. Too cold!”) also when the temperature is 24, or lower. However, something is not quite righthere. IfT is24 (or lower), the first conditionwill evaluate toFalse, and 5 Somereadersmayperhapsbepuzzledbythissentence,bringinginsuchahugethingaslife itself. The truth is, that this sentence isasdeep as it appears. My dear co-author Hans PetterLangtangen wrote thissentencewell intohiscancer treatment.HansPetterpassed awayonOctober10th,2016, a few months after the1st edition of thisbook was published.
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