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

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

Image of the Page - 32 -

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

Text of the Page - 32 -

32 1 TheFirstFewSteps PrintingStrings thatSpanMultiple Lines Ahandywaytoprintstrings thatrunoverseveral lines, is touse tripledouble- quotes (or, alternatively, triple single-quotes) like this: print("""This is a long string that will run over several lines if we just manage to fill in enough words.""") Theoutputwill then read This is a long string that will run over several lines if we just manage to fill in enough words. 1.6.4 UserInput Computerprogramsneed a set of input data and the purpose is to use these data to computeoutput (data), i.e., results. We have previously seen how input data can be provided simply by assigning values to variables directly in the code. However, to changevalues then,onemust changethemin theprogram. There are more flexible ways of handling input, however. For example through some dialogue with the user (i.e., the person running the program). Here is one example where the program asks a question, and the user provides an answer by typingon thekeyboard: age = int(input(’What is your age? ’)) print(’Ok, so you’re half way to {}, wow!’.format(age*2)) Inthefirst line, therearetwofunctioncalls,first toinputandthentoint.Thefunc- tion callinput(’What is your age? ’)will cause the question “What is your age?” to appear in the lower right pane. When the user has (after left-clicking the pane)typedinanintegerfor theageandpressedenter, that integerwillbereturned byinputasa string (sinceinputalways returnsa string22).Thus, that stringmust beconvertedtoan integerbycallingint,before theassignment toage takesplace. So, after having interpreted and run the first line, Python has established the variableageandassignedyour input to it.Thesecondlinecombinesthecalculation of twice theagewith a messageprintedon thescreen.Try these two lines in a little test programtosee foryourselfhowit works. It ispossible togetmoreflexibility intousercommunicationbybuildingastring before input shows it to the user. Adding a bit to the previous dialogue may illustratehowitworks: # ...assume the variable "name" contains name of user message = ’Hello {:s}! What is your age? ’.format(name) age = int(input(message)) print(’Ok, so you’re half way to {}, wow!’.format(age*2)) 22 Theinput function here in Python 3.6, corresponds to theraw_input function inPython 2.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