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.
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.
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.
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.
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.
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().
+ joins instead of adds.Next lesson: Booleans and Comparisons.
Work through the slides with Helio's narration, or download them:
Open the slidesDownload PowerPointDownload PDF
Run the lesson's example with real Python, right in your browser (Python loads from the internet, so you need to be online).
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.
Five quick questions on this lesson. Take the Lesson 7 quiz
Enroll free in the full course — same 17 lessons, with your progress saved. Free, no payment.