Asking questions with input()
🌍 In real life. At a bank, a form asks "Name: ____" and "Age: ____". You fill in each blank in your own handwriting, and the clerk reads what you wrote. Notice that even your age is written: on paper, everything is text until someone reads it as a number.
So far, your programs have printed the same thing every time. input() is the blank on the form: it lets a program ask the user something.
The idea in one sentence: input() shows a prompt, waits for the user to type a line and press Enter, and gives back what they typed as a str.
Its words:
- user: the person running your program
- prompt: the question
input()shows, like"Name? " - gives back: the typed text comes out of
input(), so you can keep it in a variable
Step by step, for name = input("Name? "):
- Python shows the prompt
Name?and waits. - The user types
Rimiand presses Enter. input()gives back the string'Rimi'.=puts it into the boxname.
name = input("Name? ")
print("Hello,", name)How Python does it. Each input() asks once and reads one line: the answer ends when the user presses Enter. While a program waits in input(), nothing else happens; the lines after it run only once there's an answer. Ask two questions, and Run asks you twice, in order.
Here's the catch: input() always gives back a str, even when the user types digits. Python can't know whether "12" is an age, a house number or a password, so it keeps exactly what was typed, as text.
name = input("Name? ")
city = input("City? ")
print(name, "lives in", city)age = input("Age: ")
print(type(age))
print(age + age)While a program waits in input(), nothing else happens: the lines after it run only once you answer. Changed your mind? Press ⏹ Stop, and the program ends right there.
name = input("Name? ")
print("Hi", name)Mistakes and edge cases.
- No space at the end of the prompt. With
input("Name?"), the answer sticks to the question:Name?Rimi. End the prompt with a space. - Not keeping the answer.
input("Name? ")on a line by itself asks, then throws the answer away. Put it in a variable. - Doing maths with the answer.
"12" + "12"is"1212", and"12" + 1givesTypeError: can only concatenate str (not "int") to str. The next lesson fixes this. - Printing the prompt yourself.
print("Name?")and theninput()shows the question on its own line; give the question toinput()instead.
Quiz
What does
input()give back?- Always a str
- A number, if you type digits
- Nothing: it only prints
The user types 7 at
n = input(). What isn?- "7", a str
- 7, an int
- 7.0, a float
Two answers from input() are both text. What does this print?
a = "5" b = "5" print(a + b)A program has two
input()calls. How many times will Run ask you to type something?- Twice, once for each call
- Once, for both answers together
- Never: input() fills itself in
Which line has a mistake?
name = input("Name? ") print("Hi" name)
Coding tasks
Ask for the user's name with
input()and greet them. If they typeSam, print:Hello, Sam!Press Run and type a name to try it.
Ask for a first name and then a last name (two
input()calls). Print the full name with a space in between:Rimi Akter.