Lesson 7: Talking to Your Program: input()


What you'll learn in this lesson


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().


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.

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?

Key takeaways

Next lesson: Booleans and Comparisons.


Slides

Work through the slides with Helio's narration, or download them:

Open the slidesDownload PowerPointDownload PDF

Try it yourself

Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).

Open the demo

Practice file

Download lesson07-practice.pyDownload the solution

# 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)

Save the file 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.

Check yourself

Five quick questions on this lesson. Take the Lesson 7 quiz

Want graded quizzes and progress tracking?

Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.