Talking to Your Program: input()


What you’ll learn in this lesson

  • How input() asks a question and waits for an answer
  • How to store what the person types in a variable
  • Why input() always gives back text, even when the person types digits
  • A friendlier print: showing text and numbers together with commas
  • How int() turns typed text into a number you can do arithmetic with

Programs that listen

Every program so far has done the same thing each time you ran it.
input() changes that: it lets your program ask a question, wait, and
use the answer.

name = input("What is your name? ")
print("Nice to meet you, " + name + "!")

Save this in a file and run it with F5, as you did in Lesson 3. The
Shell window shows the question and waits. Click in the Shell window
right after the question, type Sam, and press Enter. Output:

What is your name? Sam
Nice to meet you, Sam!

Three things to notice. First, the text inside the parentheses is the
prompt, the words shown to the person. Second, input() waits
until the person presses Enter, then hands back exactly what they
typed, as text. Third, = stores that text in name (Lesson 4), just
like any other value. End the prompt with a space so the person’s
typing doesn’t bump against your question. Run it again and type a
different name. Same program, different result. That is what makes a
program feel alive.

If the program seems frozen: it is waiting for you. Click in the
Shell window next to the question and type there. The most common
first-day mistake is typing the answer into the editor window instead
of the Shell.


What you type is always text

Here is a surprise. Run this and type 12:

age = input("How old are you? ")
print(age)
print(age + age)

Output:

How old are you? 12
12
1212

age + age printed 1212, not 24. Python treats everything input()
returns as text, even digits, and + joins text together (in Lesson
6, "5" + "3" gave "53"). To Python, your 12 is the characters
“1” and “2”, not the number twelve. So what happens if you try
arithmetic with it?

age = input("How old are you? ")
print(age + 1)

Output:

TypeError: can only concatenate str (not "int") to str

Read it slowly. Concatenate means join, str means text, and int
means whole number. Python is telling you: “I can only join text to
text, and you gave me text plus a number.” That is not a disaster. It
is a clear message pointing at the exact problem, and the fix is next.


A friendlier print: commas

So far you have joined pieces of a message with +. print() can also
take several things separated by commas. It puts a space between them
and doesn’t care whether they are text or numbers:

print("Hello,", "Sam")
print("Sam is", 12, "years old")

Output:

Hello, Sam
Sam is 12 years old

No + signs, no spaces to remember inside the quotes, and numbers
mix with text freely. Use commas when you just want to show things.
Use + when you need to build one piece of text to store.


Turning text into a number: int()

int() converts text made of digits into a whole number:

age_text = input("How old are you? ")
age = int(age_text)
print("Next year you will be", age + 1)

Type 12. Output:

How old are you? 12
Next year you will be 13

Now age holds the number 12, so age + 1 is 13. Programmers usually
put the two steps on one line, with input() inside int(). Read it from
the inside out: ask first, then convert.

age = int(input("How old are you? "))

What if the person types words instead of digits?

How old are you? twelve
ValueError: invalid literal for int() with base 10: 'twelve'

int() can only convert text that looks like a whole number, and twelve
doesn’t. A decimal like 12.5 fails too, because int() wants a whole
number. For now, type digits when a program asks for a number.
Handling bad answers comes in a later lesson.


Putting it together

name = input("What is your name? ")
age = int(input("How old are you? "))
print("Hello,", name)
print("In 10 years you will be", age + 10)

Type Sam, then 12. Output:

What is your name? Sam
How old are you? 12
Hello, Sam
In 10 years you will be 22

The first two lines ask and store the answers, converting the age to a
number. The last two use what was stored. Run it again with different
answers and the output changes while the program stays exactly the
same.


The two most common input mistakes

Typing the answer in the wrong window. The program is waiting in
the Shell, so click there to answer. Forgetting int() before doing
arithmetic.
If you see TypeError: can only concatenate str (not
"int") to str
, a typed answer is still text. Wrap it in int().

Talking to Your Program: input()

Python for Absolute Beginners · Lesson 7

1 / 16

What you'll learn

  • How input() asks a question and waits for an answer
  • How to store what the person types in a variable
  • Why input() always gives back text, even when the person types digits
  • A friendlier print: showing text and numbers together with commas
  • How int() turns typed text into a number you can do arithmetic with
2 / 16

Programs that listen

  • Until now, every run was the same — input() lets a program ask, wait, and use the answer
  • The text in parentheses is the prompt, the words shown to the person
  • input() waits until the person presses Enter, then hands back exactly what they typed, as text
  • End the prompt with a space so the typing doesn't bump against the question
3 / 16

Your first input

name = input("What is your name? ")
print("Nice to meet you, " + name + "!")

Output:
What is your name? Sam
Nice to meet you, Sam!

The text in parentheses is the prompt — end it with a space so the typing doesn't bump against the question.

4 / 16

Type in the right place

  • When the program waits, click in the Shell window and type there, then press Enter
  • If the program seems frozen, it is waiting for you
  • Typing the answer into the editor window is the most common first-day mistake
5 / 16

What you type is always text

  • Everything input() returns is text, even digits
  • + joins text together, so age + age gave 1212, not 24
  • To Python, your 12 is the characters "1" and "2", not the number twelve
6 / 16

Text plus text, in code

age = input("How old are you? ")
print(age)
print(age + age)

Output:
How old are you? 12
12
1212

age + age printed 1212, not 24 — + joins text instead of adding.

7 / 16

The TypeError

age = input("How old are you? ")
print(age + 1)

Output:
TypeError: can only concatenate str (not "int") to str

Read it slowly: concatenate means join, str means text, and int means whole number. It points at the exact problem.

8 / 16

A friendlier print: commas

print("Hello,", "Sam")
print("Sam is", 12, "years old")

Output:
Hello, Sam
Sam is 12 years old

Commas put a space between pieces and mix text and numbers freely. Use commas to show things; use + to build one piece of text to store.

9 / 16

Turning text into a number

age_text = input("How old are you? ")
age = int(age_text)
print("Next year you will be", age + 1)

Output:
How old are you? 12
Next year you will be 13

Now age holds the number 12, so age + 1 is 13.

10 / 16

One line, and when it fails

age = int(input("How old are you? "))

Output (type twelve):
How old are you? twelve
ValueError: invalid literal for int() with base 10: 'twelve'

Read it from the inside out: ask first, then convert. int() can only convert text that looks like a whole number — a decimal like 12.5 fails too.

11 / 16

Putting it together

name = input("What is your name? ")
age = int(input("How old are you? "))
print("Hello,", name)
print("In 10 years you will be", age + 10)

Output:
What is your name? Sam
How old are you? 12
Hello, Sam
In 10 years you will be 22

Run it again with different answers — the output changes while the program stays exactly the same.

12 / 16

Hands-on exercise

  • Ask for your favorite food and print it followed by "is a great choice!"
  • Ask for your name and print how many letters it has, using len() — like "Your name has 3 letters"
  • Ask for the current year and your birth year, convert both with int(), and print your age in years
  • Run a program and type twelve where it wants a number — read the error and explain it in one sentence
13 / 16

Key takeaways

  • input("question ") shows a prompt, waits for Enter, and returns what was typed
  • Store the answer in a variable, like any other value
  • Everything input() returns is text, even digits, so + joins instead of adds
  • print() with commas shows text and numbers together, with a space between them
  • int() turns digit text into a whole number; int(input(…)) asks and converts in one line
  • A TypeError means text and a number were mixed
  • A ValueError from int() means the text didn't look like a whole number
14 / 16

Take the quiz

Take the Lesson 7 quiz — 5 quick questions to check what you learned.

15 / 16

Next lesson

Booleans and Comparisons

16 / 16

Press play on any slide and the lesson plays on by itself from there. You can still click Next or click a slide to move on.

Download the Lesson 7 slides (PowerPoint, with narration)

Download the Lesson 7 slides (PDF)


Hands-on exercise

  1. Write a program that asks for your favorite food and prints that
    food followed by "is a great choice!".
  2. Write a program that asks for your name and prints how many
    letters it has, using len() from Lesson 6. Make the output read
    like "Your name has 3 letters".
  3. Ask for the current year and your birth year, convert both with
    int(), and print your age in years.
  4. Run one of your programs and type twelve where it wants a number.
    Read the error and explain in one sentence what it means.

Lesson 07: Talking to Your Program: input()

This is a simulation of input() — in IDLE, input() waits for you to type in the Shell; here, type your answers in the boxes and press Run. A browser page cannot wait for typing the way IDLE does, so the answers are collected first and then fed to the program. Run the same code in IDLE to see the real thing.

In the lesson, the program asks for your name and your age, converts the age to a number with int(), and prints a greeting and your age in 10 years. Change the answers and run again.




Try this: Change the age to twelve and press Run again — int() cannot convert it, and you will see the ValueError from the lesson.

The code that runs


Output

Press Run to see the output here.

Practice file

Save this as lesson07-practice.py and run it with F5 in IDLE. On a Mac, if F5 does nothing, use Run, then Run Module, from the menu.

# Lesson 7: Talking to Your Program
# WHAT TO DO: input() asks a question and gives back whatever the person
# types, as text. This program should greet the person by name, but one
# line is wrong: it prints the plain word name instead of what was typed.
# Fix line 13: build the greeting from the variable name.
# HINT: join "Hello, " and name and "!" with + (Lesson 6). name without
# quotes means "use the stored value".
# Run with F5 in IDLE. When the program asks, click in the Shell window
# and type your name. Run it again with a different name: you are done
# when the greeting changes to match each name you type.

name = input("What is your name? ")
greeting = "Hello, name!"  # <--CHANGE THIS LINE
print(greeting)

Key takeaways

  • input("question ") shows a prompt, waits for the person to press Enter, and returns what they typed.
  • Store the answer in a variable, like any other value.
  • Everything input() returns is text, even digits, so + joins instead of adds.
  • print() with commas shows text and numbers together, with a space between them.
  • int() turns digit text into a whole number; int(input(...)) asks and converts in one line.
  • A TypeError means text and a number were mixed.
  • A ValueError from int() means the text didn't look like a whole number.

Quick quiz

  1. What does input("Name? ") do?
  2. A person types 7 into x = input("Number? "). What does x hold?
  3. If age = input("Age? "), what happens when Python runs print(age + 1)?
  4. Which line asks for a whole number and stores it as a number?
  5. What does print("Sam is", 12, "years old") show?

Next lesson: Booleans and Comparisons.