Skip to content

Asking the user: input()

3 min read

Until now, every value came from you, the programmer. By the end of this lesson your programs will ask the person using them a question and use the answer. You'll also meet the single most common bug in this module, and learn why it happens so you never lose an hour to it.

Asking a question

input() shows a question, waits for the user to type an answer, and gives you back what they typed. You give that answer a name so you can use it.

name = input("What's your name? ")
print(f"Selam, {name}!")

In the editor, press Escape then Tab to move on.

One answer per line, given to input() in order.

The text inside the parentheses is the . Python prints it exactly as written, so end it with a space. Otherwise the answer sticks to the question:

first = input("Name:")
second = input("Name: ")

In the editor, press Escape then Tab to move on.

One answer per line, given to input() in order.

Everything typed is text

This program asks for two numbers and adds them. The user types 5, then 3. Predict what it prints.

What will this print?

Decide before you look. Guessing wrong is how this sticks.

a = input("First number: ")
b = input("Second number: ")
print(a + b)

When it asks, the user types 5, then 3.

Python prints

First number: 5
Second number: 3
53

input() always gives back text, even when the user types digits. So a is "5" and b is "3", and + on text joins them: 53.

If you expected 8, you're in good company: this is the bug nearly every beginner writes. Step through it and look at the values: they're in quotes, so they're strings.

Step through it

Press Next to run one line at a time and watch each name change.

When it asks, the user types 5, then 3.

a = input("First number: ")
b = input("Second number: ")
print(a + b)
Names and their values after line 1
NameValue after line 1
a'5'new
bnot made yet

Output so far

First number: 5

Step 1 of 3

Step 1 of 3. Line 1: a = input("First number: "). a is now '5'. Printed First number: 5.

The rule is simple and has no exceptions: input() always returns a string. To do arithmetic, turn the text into a number with int(), for whole numbers:

a = int(input("First number: "))
b = int(input("Second number: "))
print(a + b)

In the editor, press Escape then Tab to move on.

One answer per line, given to input() in order.

Read int(input(...)) from the inside out: input gets the text, then int turns it into a whole number. Change the answers in the Input box and run it again.

Quick check

The user types 20. After age = input(), what is type(age)?

When conversion fails

int() only accepts text that looks like a whole number. If the user types something else, the program stops:

price = int(input("Price in birr: "))
print(price * 2)

In the editor, press Escape then Tab to move on.

One answer per line, given to input() in order.

Change int to float in the code box above and run it again: it prints 11.0. Later you'll learn to ask again instead of stopping: Module 3 keeps asking until an answer is valid, and Module 7 catches this error so a typo can't stop the program.

Exercise

How old this year?

Ask for a name and a birth year, then print how old that person turns in 2026. The Input box is ready with Dawit and 2004.

Your code

# Ask for the name, then the birth year as a number

In the editor, press Escape then Tab to move on.

One answer per line, given to input() in order.

Show a solution

This is one way to solve it, not the only one. If yours prints the same thing, it works.

name = input("Name: ")
birth_year = int(input("Birth year: "))
age = 2026 - birth_year
print(f"{name} turns {age} in 2026.")

Key takeaways

  • input("Question: ") shows the question and returns what the user typed. End the prompt with a space.
  • input() always returns a string, even when the user types digits. "5" + "3" is "53".
  • Convert with int() for whole numbers or float() for decimals before doing arithmetic.
  • Text that isn't a number gives a ValueError; the message shows exactly what was typed.