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:
a = 2
b = 3
c = a + b
print('The sum is', c)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.
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:
Everything after that line waits. Nothing else runs until something is typed.
Whatever the user types is collected, right up until they press Enter.
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:
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.
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:
<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:
a = input()
b = input()
print(a + b)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:
'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:
Ages, marks, quantities, roll numbers — anything counted.
Prices, heights, percentages, averages — anything measured.
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:
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 numberHere is the adding program, finally working for any two numbers. Run it and it stops twice — type a number each time it asks:
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.
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.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
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.Why is a program with its values written into the code not very useful?
A user types 25. What does age = input() put in the box?
A user types 10 and then 5. What does print(a + b) show if a and b came straight from input()?
The user will type a price like 49.5. Which line should you use?