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

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

Bild der Seite - 32 -

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

Text der Seite - 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.
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