4.9. User Input¶
Run the code below, then change the value of
name to make the program
greet you by name!
To print a greeting for a specific user,
print("Hello Dave!") only works if
Dave is the actual user. To greet someone else, we could change the string
() to be
'Hello Sarah!' or
'Hello Elastigirl!' or any
other name we need.
Similarly, to make the statement
print("Hello, " + name + "!") work for
different users, we need to go into the code and change the string we assign to
However, what if we do not know the name of the user beforehand?
It would be great if we could ask the user of our program to enter a name, save that string, and then print a personalized greeting.
Of course, Python gives us a way to do this!
4.9.1. Requesting Data¶
To personalize the greeting, we need input from the user. This
involves displaying a prompt on the screen (e.g.
Please enter a number:), and then
saving the response from the user. Whatever information the user
enters can be stored in a variable.
Python has a built-in function to collect information from a user. As you might
expect, it is called
variable_name = input('User prompt...')
input tells Python to display the prompt in the console. It
then waits for the user to type in some information. Once the user taps Enter
or Return, the data is collected and stored in the variable.
There is a lot going on here behind the scenes, but for now you should follow this bit of wisdom:
I turn the key, and it goes.
Most of us do not need to know exactly how cars, phones, or microwave ovens
work. We just know enough to use them in our day to day lives. Similarly, we
do not need to understand how
input collects data from the console. We
just need to know that it does if we use the correct syntax.
18.104.22.168. Try It!¶
In the editor above:
- Replace line 2 with
name = input('Please enter your name: ').
- Run the program again. You should see the text
Please enter your name:appear in the black output box (the console).
- In the console, type in a name, tap Enter, and examine the result.
- Run the program several more times and enter different names.
Try adding another
+ name term inside the
other_name, then print both names using
4.9.2. Using the Collected Data¶
After collecting a name, the program does not actually DO anything with the
information. If we want to use the data, we need to tell Python what to do with
By storing the user’s name inside the variable called
name, we gain the ability to hold onto
the data and use it when we want.
Write a program that requests a user’s first and last name, then prints an output that looks like:
First name: Elite Last name: Coder Last, First: Coder, Elite
There is one very important quirk about the
input function that we need to
print(7 + 2), the output would be
Now explore the following code, which prompts the user for two numbers and then prints their sum:
Do you see the output you expected?
If we enter
2, we may expect an output of
9. The result printed is
72. What gives?!?!?
The quirk with the
input function is that it treats all entries as
strings, so numbers get concatenated rather than added. Concatenation
means that the second string gets attached to the end of the first.
"ABC" + "def" outputs as
as the string
Python treats input data as strings!
If we want our program to perform math operations on the entered numbers, we must use type conversion to change the string values into numbers.
In the print statement, use
num_2from strings to integers. Run the program and examine the result.
Instead of collecting
num_1as a string and then converting it later, we can do this in a single step. In line 1, place
input("Enter a number: ")inside the
int()function like this:
int(input("Enter a number: ")).
Repeat step 2 for
intfunctions from the print statement. Run the program and examine the result.
What happens if you enter
4.33instead of a whole number?
4.9.4. Check Your Understanding¶
What is printed when the following program runs?
1 2 3 4
user_age = input("Please enter your age: ") # The user enters 25. print(type(user_age))
Assume you want the user to enter a decimal value, like
4.33. Which of
the following statements would throw an error after the user taps Return?
input('Enter a decimal value: ')
float(input('Enter a decimal value: '))
int(input('Enter a decimal value: '))