Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Look at our program. Which lines collect information from the user?
The two lines with input(). They ask for the first and the second number.
Good, that is the input. Which line does the actual work?
total = a + b. It adds the numbers and stores the answer.
That is the processing. And the last line?
print() shows the answer on the screen, so that is the output.
Exactly: input, process, output. Almost every program you write will follow this pattern.
Riya left out int() and wrote a = input() and b = input(). She typed 5 and 3. What do you think it printed?
Eight?
It printed 53! Why would that happen?
Because input() gives back text, and adding two pieces of text just joins them together.
Right. So how do we fix it?
Wrap input() inside int(), so the text is turned into a number before we add.
We have tested 5 and 3. What other inputs should we try?
A negative number, like −4 and 10. It should print 6.
Good. What about 2.5 and 1.25?
int() cannot read 2.5, so we should use float() for decimal numbers.
And if someone types the word 'five'?
Python stops with a ValueError, because it cannot turn 'five' into a number.
Exactly. Testing with unusual inputs is how programmers find problems before users do.
the program takes two numbers in, adds them, and prints the result.
whatever the user types comes back as a string, even if it looks like a number.
int() turns text into a whole number and float() into a decimal number, so + can add them.
a, b and total store values so later lines can use them.
trying normal, negative, zero and wrong inputs shows whether a program really works.
Adding two whole numbers typed by the user (with 5 and 3 it prints The sum is: 8):
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("The sum is:", a + b)Adding two prices with decimals using float() (with 5 and 3 it prints Total bill: 8.0):
price1 = float(input("Price of first item: "))
price2 = float(input("Price of second item: "))
print("Total bill:", price1 + price2)Checking the logic with fixed values before asking the user, which prints 25 + 17 = 42:
a = 25
b = 17
print(a, "+", b, "=", a + b)Extending the idea to three numbers (with 5, 3 and 10 it prints The sum is: 18):
a = int(input("First number: "))
b = int(input("Second number: "))
c = int(input("Third number: "))
print("The sum is:", a + b + c)Input is text, so convert it first; add after int, or your sum is the worst.
A robot cashier was given the notes "5" and "3" and proudly announced a bill of 53 rupees. The shopkeeper laughed and taught it to read the notes as numbers first with int(). After that, the robot always said 8.
I-P-O: Input the numbers, Process the sum, Output the answer.
R.C.A.P. - Read with input(), Convert with int(), Add with +, Print the result.
Term
What type of value does input() return?
Click to flip
Term
What does print("5" + "3") show?
Click to flip
Term
Which function converts text to a whole number?
Click to flip
Term
Which function should you use for numbers like 2.5?
Click to flip
Term
What error appears if the user types "five" for int(input())?
Click to flip
A named place in memory that stores a value, such as a or total.
A Python function that shows a message, waits for the user to type and returns what was typed as text.
A function that converts a value, such as the text "5", into a whole number.
A function that converts a value into a decimal number, such as 2.5.
A function that displays values on the screen.
A piece of text in Python, such as "Hello" or "5".
The error Python gives when a value cannot be converted, for example int("five").
A line beginning with #, written for people to read and ignored by Python.
A step-by-step look at a Python program that reads two numbers from the user, adds them and prints the sum: the classic input, process and output program.
Adding two numbers is usually the first complete program a learner writes, and it contains the three parts of almost every program: input, processing and output. This lesson goes through the program line by line, explains why the text returned by input() must be converted with int() before adding, shows how float() handles decimal numbers, and practises testing the program with different inputs, including the error Python gives when the user types something that is not a number.
# Program to add two numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
total = a + b
print("The sum is:", total)If the user types 5 and then 3, the program prints The sum is: 8.
#, so it is a comment: a note for people that Python ignores.input("Enter first number: ") shows the message and waits for the user to type something and press Enter.int(...) turns what was typed into a whole number, and a = ... stores it in a variable called a. The second line does the same for b.total = a + b is the processing step: it adds the two numbers and stores the answer in total.print("The sum is:", total) is the output step. The comma tells print() to show both items with a space between them.The parent lesson calls the result sum. That works, but sum is also the name of a function built into Python, so a name such as total is a safer habit.
input() always gives back text (a string), even when the user types digits. Adding two pieces of text joins them instead of adding them:
a = "5"
b = "3"
print(a + b)
print(int(a) + int(b))The first print shows 53, because the text "5" and "3" are joined. The second shows 8, because int() has turned them into numbers first.
int() works only for whole numbers. For prices, heights or marks with decimals, use float():
x = float(input("Enter first number: "))
y = float(input("Enter second number: "))
print("The sum is:", x + y)With 2.5 and 1.25 this prints The sum is: 3.75. With 5 and 3 it prints The sum is: 8.0, because the result is a decimal number.
Every program in this chapter follows the same pattern:
A good programmer tries several inputs, not just one:
five instead of 5, int() cannot convert it, and Python stops with a ValueError. Later chapters show how to handle such mistakes.