Reading User Input with input()
Until now, your programs talked and never listened. Time to hand the user a microphone.
Every program so far has printed the same thing every time it ran, because every value was typed right into the code. Real programs ask questions. A quiz asks for your answer. A calculator asks for numbers. A game asks for your name.
In Python, asking a question and waiting for the answer takes one function: input().
A waiter taking your order
Think of a waiter at a restaurant. They come to your table, ask a question, and then wait, notepad ready, until you answer. They don't walk away halfway through your sentence, and they don't guess.
input() is your program's waiter. When Python reaches it, the program pauses and waits until the user types something and presses Enter. Then input() hands back exactly what they typed.
Asking a question
name = input("What's your name? ")
print("Nice to meet you,", name)When you run it, the question appears, and the program waits. If the user types Maria and presses Enter, the terminal looks like this:
What's your name? Maria
Nice to meet you, Maria- The text inside
input(...)is the prompt: the question shown to the user. It's optional, but always include one, so the user knows what you're waiting for. - Notice the space at the end of
"What's your name? ". Without it, the user's answer would be squashed right up against the question mark. input()gives back what the user typed, without the Enter key, and you store it in a variable like any other value.
Input is always text
Here's the one rule to remember about input(): it always gives back a string, even when the user types digits.
age = input("How old are you? ")
print(age + 1) # error: TypeError: can only concatenate str (not "int") to strThe user typed 17, but age holds the text "17", not the number 17. And as you saw in Numbers, Strings, and Booleans, Python won't add a number to text.
The fix is the conversion you learned in Type Conversion. Wrap input() in int() or float():
age = int(input("How old are you? "))
print("Next year you'll be", age + 1)How old are you? 17
Next year you'll be 18Read the first line from the inside out: input() asks and gets the text "17", then int() turns it into the number 17, then it's stored in age.
Use int() for whole numbers, like ages and counts, and float() for anything that might have a decimal point, like prices, heights, and test scores.
When the user types something unexpected
What if someone types seventeen when you asked for their age?
How old are you? seventeen
Traceback (most recent call last):
...
ValueError: invalid literal for int() with base 10: 'seventeen'int() can't turn that into a number, so the program crashes with a ValueError. Users type unexpected things all the time. For now, write clear prompts that say what kind of answer you want, like "How old are you? (a number) ". In Exceptions, you'll learn to catch this problem and politely ask again instead of crashing.
Cleaning up the answer
People often type extra spaces by accident, or use capital letters when you didn't expect them. Two string methods tidy that up:
.strip()removes spaces from both ends..lower()turns every letter into lowercase.
answer = input("Do you want to continue? (yes/no) ").strip().lower()
print("You said:", answer)Do you want to continue? (yes/no) YES
You said: yesThe user typed YES with spaces in front of it, but after .strip().lower(), the program only has to check for "yes". You'll meet many more string methods in Working with Strings.
No leftover-Enter trap
If you've taken the Java track, you might remember the classic trap there: reading a number, then a line of text, and getting an empty string because the Enter key was left behind. Python doesn't have that problem. Every input() reads one whole line, including the Enter, and gives back everything before it. You can mix numbers and text freely.
Try it
This program asks three questions and prints a summary. Suppose the user types Maria, then 11, then 92.5. Predict exactly what the terminal will show, including the lines the user typed.
Running Python here
Running Python right in the browser is coming to Syntaxia soon, and reading typed answers needs a real terminal anyway. If you have Python installed on your computer (see Setting Up), save this as main.py, run it with python main.py (or python3 main.py on macOS and Linux), and type your answers when it asks.
Check your prediction
What's your name? Maria
What grade are you in? 11
What was your last test score? 92.5
Hi, Maria!
Next year you'll be in grade 12.
You need 7.5 more points for a perfect score.The words after each question mark are what the user typed. The empty print() prints a blank line before the summary. name + "!" joins the exclamation mark straight onto the name with no space, and end=".\n" puts a period at the end of the grade line instead of starting a new line right away.
Try it yourself
- Run the program on your computer and answer with your own details.
- Add a fourth question, "What's your favorite subject?", and print it as part of the summary.
- Run it again and type
elevenfor the grade. Which line fails, and what's the name of the error?
Check your understanding
Up next
Your programs can now take in text from a user. What can you do with it? Plenty: measure it, search it, cut it up, and change it. That's Working with Strings.
