32
1 The First Few Steps
Printing Strings that Span Multiple Lines
A handy way to print strings that run over several lines, is to use triple doublequotes (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.""")
The output will 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 User Input
Computer programs need a set of input data and the purpose is to use these data to
compute output (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
change values then, one must change them in the program.
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
typing on the keyboard:
age = int(input(’What is your age? ’))
print(’Ok, so you’re half way to {}, wow!’.format(age*2))
In the first line, there are two function calls, first to input and then to int. The function call input(’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) typed in an integer for the age and pressed enter, that integer will be returned
by input as a string (since input always returns a string 22 ). Thus, that string must
be converted to an integer by calling int, before the assignment to age takes place.
So, after having interpreted and run the first line, Python has established the
variable age and assigned your input to it. The second line combines the calculation
of twice the age with a message printed on the screen. Try these two lines in a little
test program to see for yourself how it works.
It is possible to get more flexibility into user communication by building a string
before input shows it to the user. Adding a bit to the previous dialogue may
illustrate how it works:
# ...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 The input function here in Python 3.6, corresponds to the raw_input function in Python 2.7.
1 The First Few Steps
Printing Strings that Span Multiple Lines
A handy way to print strings that run over several lines, is to use triple doublequotes (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.""")
The output will 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 User Input
Computer programs need a set of input data and the purpose is to use these data to
compute output (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
change values then, one must change them in the program.
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
typing on the keyboard:
age = int(input(’What is your age? ’))
print(’Ok, so you’re half way to {}, wow!’.format(age*2))
In the first line, there are two function calls, first to input and then to int. The function call input(’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) typed in an integer for the age and pressed enter, that integer will be returned
by input as a string (since input always returns a string 22 ). Thus, that string must
be converted to an integer by calling int, before the assignment to age takes place.
So, after having interpreted and run the first line, Python has established the
variable age and assigned your input to it. The second line combines the calculation
of twice the age with a message printed on the screen. Try these two lines in a little
test program to see for yourself how it works.
It is possible to get more flexibility into user communication by building a string
before input shows it to the user. Adding a bit to the previous dialogue may
illustrate how it works:
# ...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 The input function here in Python 3.6, corresponds to the raw_input function in Python 2.7.
