Basic

A value can come from outside

So far every value was written in the code. A program also takes values from the outside, and that is what makes it run differently each time. The value that arrives has a kind too, and it is almost always text.

What you will see

ValueError

About 2 min of reading

Try It

Read a value from the keyboard and use it.

1

Read a name and greet it. The typed text is stored as text.

name = input("Your name: ") print("Hello, " + name)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Hello, Ada
2

Read a number and add to it. This fails, because the value is text.

age = input("Age: ") print(age + 1)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
TypeError
The error names the mismatch between text and a number. Reading that message is the skill: it tells you a conversion is missing, not that your maths is wrong.
3

Wrap the read in int() so the kind changes before the add.

age = int(input("Age: ")) print(age + 1)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
31
4

Feed a value that is not a number. The conversion refuses rather than guessing.

age = int("twelve")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
ValueError
int() only accepts text that really is a number. Real programs check or catch this before trusting input, which is what turns a crash into a message.

Data Flow

The program pauses, a value arrives from outside as text, you store it, and convert it before computing.
1 input waits for you
↓
2 text arrives
↓
3 you store it under a name
↓
4 you convert before computing
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

name = input("Your name: ") sets nothing on screen
print("Hello, " + name) Hello, Ada
Why this works
The outside is text

A keyboard, a file, and a web form all hand over characters, so anything read in arrives as text even when it looks like a number. That is why almost every program that reads a number converts it. The conversion is not decoration, it is a change of kind.

Why this matters now

A program with every value written inside it is a fixed calculation. A program that reads a value is a general method, one that works for any input. That is the difference between a calculation and a tool, and reading input is the smallest step that makes it.

Examples

Ask and receive

name = input("Your name: ")
print("Hello, " + name)

input pauses and waits for you to type. Whatever you type becomes a text value, stored under name. The program then works on a value it never knew in advance.

Arrived as text

age = input("Age: ")
print(age + 1)

This fails. The value that arrived is text, so age holds "30" not 30, and text plus a number is refused. The fix is to convert before computing, which the next lesson covers.

Convert then compute

age = int(input("Age: "))
print(age + 1)

int() turns the text into a whole number, so the add works. The conversion is the bridge between the outside world, which is all text, and the numbers your program needs.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Text is a value you can build
Previous Lesson
Text is a value you can build
Next Lesson
Changing a value kind on purpose
Changing a value kind on purpose →