LambdaLabTM
Computer Science · Class 11 · Variables & User Input
input()int() and float()⏱️ 9 min read

Taking Input from the User

Every program you have written so far already knows its own answer. The numbers are typed into the code, so the program can only ever work out that one sum. In this lesson you hand the choice over to the person using the program.

1A program that can only add 2 and 3

Here is a program that adds two numbers. It works, and the answer is right:

fixed_sum.py
a = 2
b = 3
c = a + b
print('The sum is', c)
Output
The sum is 5

Now ask a harder question: who is this program for? It adds 2 and 3. It will add 2 and 3 today, tomorrow, and every time anyone ever runs it. If your friend wants the sum of 47 and 68, this program is of no use to them at all.

They could open the file and edit the two lines. But then they are not using your program, they are rewriting it — and they would need to know Python to do it. A program that has to be edited before each use is barely a program.

The real problem
The values are hard-coded — written into the code itself. So the program solves exactly one sum, the one you chose while writing it. Wouldn't it be far better if the person running the program could supply the two numbers of their own choice? The same program would then work for everybody, for any two numbers. That is what makes a program general — and useful.

2input() lets the user choose

Python gives us a function for exactly this: input(). It allows the person running the program to enter a value of their own choice, while the program is running. Not while it is being written — while it is running.

When Python reaches an input(), three things happen:

The program stops

Everything after that line waits. Nothing else runs until something is typed.

It reads the keyboard

Whatever the user types is collected, right up until they press Enter.

It hands the value back

The typed value comes into the program, and = saves it in a variable.

Watch that trip below. Use Next step to take it one stage at a time, or Play all to see it run straight through. You can change what gets typed before you start:

One trip from the keyboard into a box
Program pauses
You type
input() takes it as text
Stored in the variable
age = input()
print(age)
Keyboard
Press Start
What input() hands back
nothing yet
Memory

Take it a step at a time with Next step, or watch the whole trip with Play all. The program will reach input() and stop dead until something is typed.

Key Takeaway
input() pauses the program, takes whatever the user types on the keyboard, and brings it into the program as a value — which = then stores in a variable. Look closely at what lands in the box: it arrives as text, with quotes around it.

3Whatever you type arrives as text

This is the part that catches everybody. input() does not care what the typing looks like. Digits, letters, spaces — it collects the keystrokes and hands them back as one piece of text, which Python calls a string.

So if the user types 25, the variable does not hold the number 25. It holds the text '25'. You can check this yourself with type(). Press Run: the program will stop at input() and a box will appear in the output — type a number there and press Enter, exactly as a real user would:

what_type.py

<class 'str'>str is Python's name for text. Even though you typed digits and nothing else, what came back was text.

4Which breaks our sum

Now put input() into the adding program and watch it go wrong. The user types 10 and 5, and the program prints 105:

broken_sum.py
a = input()
b = input()
print(a + b)
Output
10
5
105

Nothing is broken and there is no error — which is what makes this so confusing. The reason is that both boxes hold text, and + does a different job depending on what it is given. Given two numbers it adds them. Given two pieces of text it joins them end to end.

Flip the switch and watch the same two keystrokes give two answers:

The same two keystrokes, added two ways
a = input() # you type 10
b = input() # you type 5
print(a + b)
'10'str+'5'str='105'
Both boxes hold text, so + joins them end to end. The digits 10 and 5 are pushed together into one longer piece of text, '105'. Nothing was added up — Python never saw two numbers. Joining text like this is called concatenation, and it has a chapter of its own later.
Joining is not adding
'10' + '5' gives '105': the two pieces of text are stuck together into one longer piece. This is called concatenation, and it is a genuinely useful thing that gets a chapter of its own later. Here it is simply not what we wanted.

5The fix: int() and float()

Python can convert text into a number, as long as the text really does look like one. There are two functions for it, and which you use depends on the kind of number you are expecting:

int()
For whole numbers

Ages, marks, quantities, roll numbers — anything counted.

int('25') → 25
float()
For decimal numbers

Prices, heights, percentages, averages — anything measured.

float('49.5') → 49.5

You wrap the input() in whichever one you need. Python works from the inside out: input() runs first and produces text, then int() turns that text into a number, and only then does = store it:

inside_out.py
a = int(input())

# Python does it in this order:
#   1. input()      → the user types 10, so this gives '10'  (text)
#   2. int('10')    → gives 10                               (a number)
#   3. a = 10       → the box now holds a number

Here is the adding program, finally working for any two numbers. Run it and it stops twice — type a number each time it asks:

working_sum.py

Run it again with two numbers of your own. The program has not changed — and it never needs to again. It now works for any two whole numbers, for anybody who runs it. That is the difference between a program that solves one sum and a program that solves the problem.

Use float() when a decimal is possible
int() refuses anything that is not a whole number: int('49.5') is an error, not 49. If the user might reasonably type a decimal — a price, a height, an average — use float() instead. float('50') works happily too, giving 50.0.
average.py
It has to look like a number
int() can only convert text that really is a number. int('hello') — or an empty box, if the user just presses Enter — gives a ValueError. For now, type sensible values; handling a user who types nonsense is a later topic.

6Recap

Key Takeaway
Values written into the code make a program that solves only one problem. input() lets the user supply values of their own choice while the program runs, which makes the program general. Whatever the user types comes back as text, so + would join it rather than add it. Wrap it in int() for whole numbers or float() for decimals, and the arithmetic works.
Quick Check

Why is a program with its values written into the code not very useful?

Quick Check

A user types 25. What does age = input() put in the box?

Quick Check

A user types 10 and then 5. What does print(a + b) show if a and b came straight from input()?

Quick Check

The user will type a price like 49.5. Which line should you use?