Lesson 17
input() - reading a number
After this lesson: You'll read a number, turn it from text into a number and calculate with it.
Video
The video for this lesson is coming soon.
Explanation
In the previous lesson we met three magic machines: int(), float() and str(). Remember? "5" is text, and 5 is a number - they look similar, but you can only calculate with the number. Today we'll join the int() machine with input() - and calculate with numbers typed on the keyboard! ๐ช
Watch out, a trap! input() always gives text - even when someone types a number. See what happens when we try to double it...
Text times 2
Type a number ๐ข (e.g. 21) and press Enter โ. See what the program prints and what type x has - input() always gives text, even when you type a number!
Type a number on the screen - let's see what times does with it.
1x = input()2print(x * 2)
โWhy was the number repeated?
The number you typed wasn't doubled - it was written twice. For example, when you type 21, input() gives the text "21" - two characters: a two and a one. Like in lesson 16: the text "21" looks like a number, but it isn't one. And text times 2 is the text repeated twice: "2121", not 42. 7 gives 77, and 100 gives 100100. Python doesn't know you meant a number!
Adding is even worse: "21" + 1 is a TypeError - text and a number can't be added. And it doesn't matter which number you type - it's always text.
You already know the fix: the int() machine!
Text plus a number
Type any number ๐ข (e.g. 99) and press Enter โ. The program stops with a TypeError - look at what's in the box x, and below we explain why it happened.
Type a number on the screen - let's see what the plus does with it.
1x = input()2print(x + 1)
โWhat happened? Step by step
Type, for example, 99:
input()puts the text'99'๐ค into the box x - you can tell by the quotes in the box.- Python reaches
print(x + 1). On the left of the plus there's text'99'๐ค, on the right a number1๐ข. - The plus can do two different things:
- it adds numbers:
99 + 1is100, - it joins text:
"99" + "1"is"991".
- it adds numbers:
- Here there's text and a number at once. Add and give
100? Or join and give991? Python doesn't guess ๐คท - it stops the program with a TypeError (wrong type).
Let's read the message piece by piece:
| Piece | What it means |
|---|---|
TypeError | wrong type - something isn't the type it should be |
can only concatenate str | I can only join text... |
(not "int") | ...not with a number... |
to str | ...only with other text |
Together: "I can only join text with text, not with a number".
How to fix it? First turn the text into a number with the int() machine - that's exactly what we'll do next!
Without int() and with int()
Type a number ๐ข (e.g. 21) and press Enter โ - the same typed line gives two different results: without int() and with int().
Type a number on the screen - you'll see two ways: without int() and with int().
1number_str = input()2print(number_str + number_str)3number = int(number_str)4print(number + number)
โint() turns text into a number
In lesson 16 we did it in two steps: first the text in one box (price_text = "3.5"), then the machine made a number out of it in a second box (price_float = float(price_text)). With input() it's exactly the same:
number_str = input()
number = int(number_str)
input()reads text, e.g."21", and puts it into the box number_str.int(number_str)makes a number out of it (from"21"- the number21) and puts it into the box number.
In this lesson we always write it on two lines: first input(), then int(). That way you can see every step.
Two steps: text and a number
Type a number ๐ข (e.g. 21) and press Enter โ. Watch the text from the box text go through the int() machine and come out as a real number in the box number. Then see what * 2 does to the text, and what it does to the number.
Type a number on the screen and press Enter - input() gives it as text.
1number_str = input()2number = int(number_str)3print("Text times 2:", number_str * 2)4print("Number times 2:", number * 2)
โA times table for any number โ๏ธ
Type any number ๐ข (e.g. 8) and press Enter โ - the program prints its times table straight away.
Type a number on the screen and press Enter - you'll see its times table in pictures.
1n_str = input()2n = int(n_str)3print(n, "x 1 =", n * 1)4print(n, "x 2 =", n * 2)5print(n, "x 3 =", n * 3)6print(n, "x 4 =", n * 4)7print(n, "x 5 =", n * 5)
โCountdown to Christmas ๐
How many days are left until Christmas? Type the number of days ๐ (e.g. 78) and press Enter โ - the program turns it into weeks and hours.
Type a number of days on the screen and press Enter - you'll see them on a calendar.
1days_str = input()2days = int(days_str)3print("Christmas is in", days, "days")4print("That is", days // 7, "weeks and", days % 7, "days")5print("Or", days * 24, "hours!")
โTwo numbers on two lines
When the input has two numbers, each on its own line, we do the two steps for each number: input() reads one line, and int() makes a number out of it:
first_str = input()
first_number = int(first_str)
second_str = input()
second_number = int(second_str)
The sum of two numbers
Type two numbers ๐ข, each on its own line (e.g. 6 and 7). Press Enter โ after each number.
Type number 1 on the screen and press Enter - input() gives it as text.
1first_str = input()2first_number = int(first_str)3second_str = input()4second_number = int(second_str)5print(first_number + second_number)6print(first_number * second_number)
โA real task: candies
Type the number of candies ๐ฌ (e.g. 23), then the number of children ๐ง (e.g. 4). Press Enter โ after each number. The program shares the candies for any numbers - only the children can't be 0, because you can't divide by zero (ZeroDivisionError).
Type number 1 on the screen and press Enter - input() gives it as text.
1candies_str = input()2candies = int(candies_str)3children_str = input()4children = int(children_str)5print(candies // children)6print(candies % children)
โA garden ๐ป
Type the garden's length ๐ (e.g. 6), then its width (e.g. 4). Press Enter โ after each number. The program works out the area and the fence around it.
Type number 1 on the screen and press Enter - input() gives it as text.
1length_str = input()2length = int(length_str)3width_str = input()4width = int(width_str)5print("Area:", length * width)6print("Fence:", 2 * (length + width))
โPocket money ๐ฐ
Type how much you get every week ๐ฐ (e.g. 15), then for how many weeks you save ๐ (e.g. 8). Press Enter โ after each number.
Type number 1 on the screen and press Enter - input() gives it as text.
1pocket_money_str = input()2weekly_pocket_money = int(pocket_money_str)3weeks_str = input()4number_of_weeks = int(weeks_str)5print(weekly_pocket_money * number_of_weeks)
โAnd numbers with a dot? float(input())
From lesson 16 you also know the float() machine. When the input may be a number with a dot (e.g. a price 2.5), in the second step we use float() instead of int():
price_str = input()
price = float(price_str)
Remember: int("2.5") is a ValueError, but float("2.5") works. In AlgoBaza tasks the input is almost always whole numbers, so you'll mostly use int().
Ice cream at any price ๐ฆ
Type the price of one ice cream ๐ฆ - it can have a dot, e.g. 3.5 - and press Enter โ. float() makes a number out of it, and the program works out two ice creams.
Type a number on the screen and press Enter - input() gives it as text.
1print("How much is one ice cream?")2price_str = input()3price = float(price_str)4print("Two ice creams:", price * 2)
โCinema tickets ๐๏ธ
Type the price of one ticket ๐๏ธ - e.g. 12.5 - and press Enter โ. float() makes a number with a dot out of it, and the program works out the tickets for three people.
Type a number on the screen and press Enter - input() gives it as text.
1print("How much is one ticket?")2price_str = input()3price = float(price_str)4print("Three tickets:", price * 3)
โHow heavy is the backpack? ๐
Type two weights in kilograms โ๏ธ, each on its own line - e.g. 2.5 (books ๐) and 1.5 (pencil case and lunch ๐ฅช). Press Enter โ after each one. Every weight goes through float(), then the program adds them.
Type number 1 on the screen and press Enter - input() gives it as text.
1print("How heavy are the books?")2books_str = input()3books = float(books_str)4print("How heavy is the rest?")5rest_str = input()6rest = float(rest_str)7print("The backpack weighs:", books + rest, "kg")
โThe input has two lines: 3 and 4. What does the program print?
a = input()b = input()print(a + b)
The input is 5. What does the program print?
n_str = input()n = int(n_str)n = n * nprint(n + 1)
Common mistakes
- You forget the second line with
int()- then you calculate with text: strange results (2121) or a TypeError. - The line
int(number_str)on its own does nothing - the result must go into a box:number = int(number_str). - You calculate with the wrong box:
number_str * 2instead ofnumber * 2. number_str holds text, number holds a number.
Text or number? A comparison
Plus joins text but adds numbers. That's why int() is so important.
The same with int()
How old will you be?
Type your age ๐ and press Enter โ - the program works out how old you'll be in 5 and in 10 years. ๐
Type a number on the screen and press Enter - input() gives it as text.
1age_str = input()2age = int(age_str)3print("In 5 years:", age + 5)4print("In 10 years:", age + 10)5print("Days alive (about):", age * 365)
โThe input is 5. What does the program print?
n = input()print(n * 3)
The input is 5. What does the program print?
n_str = input()n = int(n_str)print(n * 3)
Remember
input()always gives text - even when the input is a number.- Read a number in two steps:
number_str = input(), thennumber = int(number_str). - Each number on its own line is a separate
input()and a separateint(). - A number with a dot:
float(number_str)instead ofint(number_str). - Without
int()plus joins and times repeats text.
Remember: first input(), then int() - and only then calculate. In the tasks the examples show what the input will be.
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.