livepython.net
CHAPTER 07Getting Input from the User
7.1  INPUT()

Python input(): Get Input from the User

What Is input()?

So far, your programs always did the same thing. Now they'll listen. The built-in input() function lets the user type something, and gives it to your program:

PYTHONedit me
name = input()
print("Hello,", name)
OUTPUT
Hello, Sunny
  1. input() waits for the user to type a line.
  2. Whatever they type is stored in name.
  3. The next line uses it, like any other variable.

Change the name in the Input box and watch the output change. The same program now greets anyone.

Think of input() as a waiter taking your order: the program pauses, asks, writes your answer down, and carries on.

How input works on LivePython

On your own computer, Python stops and waits while you type. On LivePython, Python runs in your browser, so you type the answers first, in the Input box, one per line. Then the code runs and reads them in order. It's the same input(), just with the answers ready in advance.

Remember chapter 4?

When you learned variables, you saw that one variable lets a program work for any name. input() is where that pays off: the value comes from the user, not from your code.

input() reads one line the user types, and gives it to your program.

your turnPredict the output

What will this print?

name = input() print("Hi", name)

The user types: Rocky

Your turn! What will this code show on the screen?
your turnFill the gap

Make the program read the user's city

city = ? print("You live in", city)

The user types: Pune

It should print: You live in Pune

Which piece makes this code work?

Show a Prompt with input()

An empty input() gives the user no clue what to type. Put a prompt inside the brackets: a short question shown before they type.

PYTHONedit me
name = input("What is your name? ")
print("Nice to meet you,", name)
OUTPUT
What is your name? Nice to meet you, Sunny

The prompt What is your name? is printed first, then Python reads the answer.

Why the output looks squashed on LivePython

On your own computer, the screen would show:

What is your name? Sunny
Nice to meet you, Sunny

The user types Sunny and presses Enter, so the answer and the new line appear on the screen. On LivePython, the answers come from the Input box, so they're never typed onto the screen. You only see the prompt, and the next print() continues right after it. The program is the same; only the screen is different.

Tips for good prompts

  • End with a space, so the answer doesn't stick to the question: "Name: ", not "Name:".
  • Say what you want, and in what form: "Age in years: ", "Price in rupees: ".
  • Keep it short.

Prompts in practice questions

Practice questions compare your output exactly, so a prompt would end up in your output and fail the test. That's why practice questions always say: use input() with no prompt.

Put a short question inside input("…") so the user knows what to type.

your turnPick the right idea

Which prompt is best?

Think about it, then pick one.

Input Is Always Text

Here's the most important fact about input(): it always gives you a string, even when the user types a number.

PYTHONedit me
age = input()
print(type(age))
OUTPUT
<class 'str'>

The user typed 25, but Python stored the text "25".

Why it matters

Remember from chapter 5: + joins strings, it doesn't add them.

PYTHONedit me
a = input()
b = input()
print(a + b)
OUTPUT
23

The user typed 2 and 3, and the program said 23! Python joined the two strings "2" and "3". To add them as numbers, you have to convert them first. That's the whole next topic.

Think of input() as a message on paper. Even if the paper says "25", it's still a piece of paper with writing on it. To do maths, you first have to read the number off the paper.

Text is fine for text

For names, cities, colours and messages, a string is exactly what you want:

PYTHONedit me
colour = input()
print(f"Your favourite colour is {colour}.")
OUTPUT
Your favourite colour is blue.

input() always returns a string, even if the user types digits.

your turnPredict the output

What will this print?

a = input() b = input() print(a + b)

The user types: 45

Your turn! What will this code show on the screen?
your turnType the output

Type what this prints

x = input() print(type(x))

The user types: 3.5

Read the code line by line, then type the output exactly.

Reading Several Inputs

Each input() reads one line. To ask several questions, call input() several times:

PYTHONedit me
name = input()
city = input()
print(f"{name} lives in {city}.")
OUTPUT
Sunny lives in Pune.

The first input() reads the first line (Sunny), the second reads the next line (Pune). On LivePython, put each answer on its own line in the Input box, in the same order as the questions.

Think of a queue at a counter: the first person in the queue is served by the first input(), the second by the second.

Not enough answers

If the code asks more questions than there are lines, Python stops:

PYTHONedit me
name = input()
city = input()
print(name, city)
OUTPUT
EOFError: EOF when reading a line

EOF means "end of file": Python reached the end of the input while waiting for another line. Add the missing answer, and it works.

Extra answers are ignored

If there are more lines than input() calls, the extra lines are simply never read. No error, but the user's extra typing is wasted.

Order matters

Python reads lines top to bottom, like everything else (chapter 1). If you swap the two input() lines in the code, name gets the city and city gets the name. Always keep your questions and your Input lines in the same order.

One input() reads one line, in order. Ask twice, answer twice.

your turnPredict the output

What will this print?

a = input() b = input() print(b, a)

The user types: redblue

Your turn! What will this code show on the screen?
your turnFix the code

Fix the order so it prints: Rocky is 12

It should print: Rocky is 12

The user types: Rocky12

age = input()
name = input()
print(name, "is", age)
This code does not work yet. Fix it, then press Run.
your turnPredict the output

What will this print?

print("a" * 3 + "b")
Your turn! What will this code show on the screen?

Practice: input()

Solve these to lock in what you learned in this topic. Each one runs in your browser.

  1. 51.Python Program to Greet the User by NameEasySolve
  2. 52.Python Program to Read Two Words and Make a SentenceEasySolve