Python 3D Academyবাংলা

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? "):

  1. Python shows the prompt Name? and waits.
  2. The user types Rimi and presses Enter.
  3. input() gives back the string 'Rimi'.
  4. = puts it into the box name.
name = input("Name? ")
print("Hello,", name)
Press Run: the program stops at input(), and a box appears right after the prompt in the output. Type your answer there and press Enter.

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)
1212, not 24! The next lesson shows how to turn text into a number.

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)
Run this, and press ⏹ Stop instead of answering: the program ends, and "Hi" is never printed.

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" + 1 gives TypeError: can only concatenate str (not "int") to str. The next lesson fixes this.
  • Printing the prompt yourself. print("Name?") and then input() shows the question on its own line; give the question to input() instead.

Quiz

  1. What does input() give back?

    • Always a str
    • A number, if you type digits
    • Nothing: it only prints
  2. The user types 7 at n = input(). What is n?

    • "7", a str
    • 7, an int
    • 7.0, a float
  3. Two answers from input() are both text. What does this print?

    a = "5"
    b = "5"
    print(a + b)
  4. 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
  5. Which line has a mistake?

    name = input("Name? ")
    print("Hi" name)

Coding tasks

  1. Ask for the user's name with input() and greet them. If they type Sam, print:

    Hello, Sam!

    Press Run and type a name to try it.

  2. Ask for a first name and then a last name (two input() calls). Print the full name with a space in between: Rimi Akter.