What is user input?
An important part of computer science is creating interactive programs. So far, your programs have probably done the same thing every time they run. User input changes that: it lets the person running your program type information into it, so the program can respond to them.
In Python, we use the input() function to ask the user a question. The text you put inside the parentheses is the prompt: the message shown to the user. The program then pauses and waits. Once the user types an answer and presses Enter, the value they typed is returned.
answer = input("Ask your question here: ")
Notice that we store the answer in a variable. That is the key move: once the answer lives in a variable, the rest of your program can use it - print it, compare it, do math with it, anything. Without a variable, the user's answer would disappear the moment they pressed Enter.
Two jobs in one line: input() shows the prompt and collects the answer. The variable on the left of the = is where the answer gets saved.
Example: input() basics
Take a look at this program to get a feel for how user input works. Press Run and read the output carefully, with these guiding questions in mind:
- What do you notice about the type of value input() returns?
- Why is it important to store the user's answer in a variable?
# Gets user input name = input("What is your name? ") num = input("What is your favorite number? ") print("") # Uses user input to print out information print("Hello " + name + "!") print(num + "?! That's my favorite number too!") print("") # Prints out the variable type print("The variable 'name' is a: ") print(type(name)) print("The variable 'num' is a: ") print(type(num))
What is your name? Karel What is your favorite number? 7 Hello Karel! 7?! That's my favorite number too! The variable 'name' is a: <class 'str'> The variable 'num' is a: <class 'str'>
Look closely at the last line. The user typed the number 7, but Python says num is a str (a string), not a number. Hold that thought - it matters a lot in a minute.
Talk to your program
The best way to understand input() is to be the user. The simulator below runs the Python code on the left, one highlighted line at a time. Every time the program reaches an input() call, it stops and waits for you to type a real answer in the chat. Press Run program to start.
Press "Run program" to start the conversation.
Try the Age bot tab too. It has a bug hiding in it - the exact bug you are about to learn about.
input() always returns a string
Here is the number one thing to remember from this tutorial: input() returns the user's answer as a string, even if the user types a number! To Python, the answer "16" is text, just like "hello" is text.
This causes trouble the moment you try to do math with the answer. Watch what happens when we ask for an age and try to add 1 to it:
age = input("How old are you? ") print("Next year you will be " + (age + 1))
How old are you? 16 Traceback (most recent call last): File "main.py", line 2, in <module> TypeError: can only concatenate str (not "int") to str
The program crashes with a TypeError. Python is saying: "You gave me the string "16" and asked me to add the number 1 to it. Strings and numbers don't add together."
See it live: switch the simulator above to the Age bot tab, run it, and watch it crash - then press Fix it with int() to see the repaired program run with your same answer.
Converting input with int() and float()
Luckily the fix is easy. The int() function converts a string like "16" into the actual integer 16. Converting a value from one type to another is called casting. Wrap it right around input() and the conversion happens the moment the user answers:
age = int(input("How old are you? ")) # now age is a number!
See int(input()) in action in this temperature converter, and explore how it works with these guiding questions:
- What would happen if you removed the int() function on line 1? Why?
- What would happen if the user typed a word instead of a number? Why?
deg_fahrenheit = int(input("What is the temperature in degrees Fahrenheit? ")) deg_celsius = (deg_fahrenheit - 32) * (5/9) print("That means the temperature in Celsius is: ") print(deg_celsius)
What is the temperature in degrees Fahrenheit? 212
That means the temperature in Celsius is:
100.0
Decimals too: int() only works for whole numbers. If the user might answer with a decimal like 98.6, use float(input(...)) instead. And if the user types something that is not a number at all, like "pizza", the conversion fails with a ValueError.
Practice: build interactive programs
Now that you know the basics of user input, try it out with these three exercises. In each program, store the information you gather from the user in variables.
Ten Years From Now
Ask the user for their age. Then print out how old they will be in 10 years. Careful: this one needs math, so remember the gotcha!
# int() converts the answer so we can do math with it age = int(input("How old are you? ")) print("In 10 years you will be", age + 10)
How old are you? 12
In 10 years you will be 22
Without int(), the variable age would be the string "12", and age + 10 would crash with a TypeError.
Hello Around the World
Ask the user for their name. Then print out three greeting statements in three different languages.
name = input("What is your name? ") print("Hello, " + name + "!") print("Hola, " + name + "!") print("Bonjour, " + name + "!")
What is your name? Karel
Hello, Karel!
Hola, Karel!
Bonjour, Karel!
No int() needed here. A name should stay a string, and joining strings with + works great.
Sum Machine
Ask the user for two numbers. Then calculate and print out the sum.
num1 = int(input("Enter your first number: ")) num2 = int(input("Enter your second number: ")) print("The sum is", num1 + num2)
Enter your first number: 8 Enter your second number: 34 The sum is 42
Without the two int() calls, num1 + num2 would glue the strings together and print 834 instead of 42. Same + sign, totally different meaning!
Quick quiz
1. No matter what the user types, what type of value does input() return?
2. The user types 12. What happens when this program runs?
age = input("Age? ") print(age + 1)
3. Which line correctly asks for a number the program can do math with?
Keep learning Python
You can now have real conversations with your programs. Next, learn to print output like a pro, or use conditionals to make your program respond differently depending on what the user says.