Topic focus: The input
function
The simplest way for a Python program to ask the user for input
is the input
function.
It's used for programs that run inside of a terminal,
and when the input
function is executed,
the program stops and waits for the user
to enter a line of text into the terminal.
The line ends when the user hits the Return key
(called the Enter key on some keyboards).
The input
function takes a single (optional) argument,
which is a prompt string which is usually a message
indicating what kind of input is desired.
If a prompt string is provided,
that string will be printed on the input line
before the location where the user will enter their input.
Here are some examples in the interactive Python interpreter:
>>> input()
this is a test <-- YOU ENTER THIS LINE
'this is a test'
>>> input("Please enter your name: ")
Please enter your name: Bob <-- YOU TYPED "Bob"
'Bob'
The first call to input()
didn't have a prompt argument,
so the user had to know to input a string at that time.
The second call had the prompt Please enter your name:
,
which makes it clear to the user
that they should enter their name
at that point in the program.
Note
One trick we used in the second call
is to add a space at the end of the prompt string
(right after name:
).
This makes the inputted string easier to see.
The input()
function returns the string that was input.
You normally would assign this to a variable
so that you can use the input string later.
>>> name = input("Please enter your name: ")
Please enter your name: Bob <-- YOU TYPED "Bob"
>>> name
'Bob'
>>> name.upper()
'BOB'
Be careful when inputting a number! The number will be represented as a string, since Python doesn't "know" that it's supposed to be a number. If you want it to be a number, you have to convert it manually.
Here's what you get if you forget to do this:
>>> age = input("Please enter your age in years: ")
Please enter your age in years: 19 <-- YOU TYPED "19"
>>> age
'19'
>>> age + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
Python is reminding you that you can't add a string ("19"
)
to an integer (1
).
The right way to do this is as follows:
>>> age = int(input("Please enter your age in years: "))
Please enter your age in years: 19 <-- YOU TYPED "19"
>>> age
19
>>> age + 1
20
Notice that we surrounded the input
function call
with the int
function;
this converts the string returned by input
into an integer
(if possible).
Aside: Nested function calls
Nested function calls like int(input(...))
are perfectly OK in Python
(and in pretty much all programming languages).
It just means that the return value of the inner function
(input
in this case)
is passed as the input to the outer function
(int
in this case).
Some novice programmers think they need to store
the return value of input
in a variable before using it,
like this:
>>> age_string = input("Please enter your age in years: ")
Please enter your age in years: 19 <-- YOU TYPED "19"
>>> age = int(age_string)
19
>>> age + 1
20
While this will certainly work, it's not necessary.