AlgoBazaAlgoBaza
Python for kids ยท Module 1

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!

?๐Ÿ”ค text
ร—
2๐Ÿ”ข number

Type a number on the screen - let's see what times does with it.

Code
1x = input()
2print(x * 2)
Screen
โ–
1/2 ยท line 1 runs next

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.

?๐Ÿ”ค text
+
1๐Ÿ”ข number
?

Type a number on the screen - let's see what the plus does with it.

Code
1x = input()
2print(x + 1)
Screen
โ–
1/2 ยท line 1 runs next

What happened? Step by step

Type, for example, 99:

  1. input() puts the text '99' ๐Ÿ”ค into the box x - you can tell by the quotes in the box.
  2. Python reaches print(x + 1). On the left of the plus there's text '99' ๐Ÿ”ค, on the right a number 1 ๐Ÿ”ข.
  3. The plus can do two different things:
    • it adds numbers: 99 + 1 is 100,
    • it joins text: "99" + "1" is "991".
  4. Here there's text and a number at once. Add and give 100? Or join and give 991? Python doesn't guess ๐Ÿคท - it stops the program with a TypeError (wrong type).

Let's read the message piece by piece:

PieceWhat it means
TypeErrorwrong type - something isn't the type it should be
can only concatenate strI 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().

๐Ÿ”ค Without int(): text + text?
๐Ÿ”ข With int(): number + number?

Type a number on the screen - you'll see two ways: without int() and with int().

Code
1number_str = input()
2print(number_str + number_str)
3number = int(number_str)
4print(number + number)
Screen
โ–
1/2 ยท line 1 runs next

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)
  1. input() reads text, e.g. "21", and puts it into the box number_str.
  2. int(number_str) makes a number out of it (from "21" - the number 21) 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.

1๏ธโƒฃ
โŒจ๏ธ number_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ number?

Type a number on the screen and press Enter - input() gives it as text.

Code
1number_str = input()
2number = int(number_str)
3print("Text times 2:", number_str * 2)
4print("Number times 2:", number * 2)
Screen
โ–
1/2 ยท line 1 runs next

A times table for any number โœ–๏ธ

Type any number ๐Ÿ”ข (e.g. 8) and press Enter โŽ - the program prints its times table straight away.

โŒจ๏ธ n_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ n?

Type a number on the screen and press Enter - you'll see its times table in pictures.

Code
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)
Screen
โ–
1/2 ยท line 1 runs next

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.

๐Ÿ“… days:?
?

Type a number of days on the screen and press Enter - you'll see them on a calendar.

Code
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!")
Screen
โ–
1/2 ยท line 1 runs next

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.

1๏ธโƒฃ
โŒจ๏ธ first_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ first_number?
2๏ธโƒฃ
โŒจ๏ธ second_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ second_number?

Type number 1 on the screen and press Enter - input() gives it as text.

Code
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)
Screen
โ–
1/2 ยท line 1 runs next

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).

1๏ธโƒฃ
โŒจ๏ธ candies_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ candies?
2๏ธโƒฃ
โŒจ๏ธ children_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ children?

Type number 1 on the screen and press Enter - input() gives it as text.

Code
1candies_str = input()
2candies = int(candies_str)
3children_str = input()
4children = int(children_str)
5print(candies // children)
6print(candies % children)
Screen
โ–
1/2 ยท line 1 runs next

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.

1๏ธโƒฃ
โŒจ๏ธ length_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ length?
2๏ธโƒฃ
โŒจ๏ธ width_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ width?

Type number 1 on the screen and press Enter - input() gives it as text.

Code
1length_str = input()
2length = int(length_str)
3width_str = input()
4width = int(width_str)
5print("Area:", length * width)
6print("Fence:", 2 * (length + width))
Screen
โ–
1/2 ยท line 1 runs next

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.

1๏ธโƒฃ
โŒจ๏ธ pocket_money_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ weekly_pocket_money?
2๏ธโƒฃ
โŒจ๏ธ weeks_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ number_of_weeks?

Type number 1 on the screen and press Enter - input() gives it as text.

Code
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)
Screen
โ–
1/2 ยท line 1 runs next

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.

1๏ธโƒฃ
โŒจ๏ธ price_str?
โžก๏ธ
โš™๏ธfloat()
โžก๏ธ
๐Ÿ“ฆ price?

Type a number on the screen and press Enter - input() gives it as text.

Code
1print("How much is one ice cream?")
2price_str = input()
3price = float(price_str)
4print("Two ice creams:", price * 2)
Screen
โ–
1/3 ยท line 1 runs next

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.

1๏ธโƒฃ
โŒจ๏ธ price_str?
โžก๏ธ
โš™๏ธfloat()
โžก๏ธ
๐Ÿ“ฆ price?

Type a number on the screen and press Enter - input() gives it as text.

Code
1print("How much is one ticket?")
2price_str = input()
3price = float(price_str)
4print("Three tickets:", price * 3)
Screen
โ–
1/3 ยท line 1 runs next

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.

1๏ธโƒฃ
โŒจ๏ธ books_str?
โžก๏ธ
โš™๏ธfloat()
โžก๏ธ
๐Ÿ“ฆ books?
2๏ธโƒฃ
โŒจ๏ธ rest_str?
โžก๏ธ
โš™๏ธfloat()
โžก๏ธ
๐Ÿ“ฆ rest?

Type number 1 on the screen and press Enter - input() gives it as text.

Code
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")
Screen
โ–
1/3 ยท line 1 runs next
?

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 * n
print(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 * 2 instead of number * 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.

"21" + "21"
1/2 ยท click the arrow - Python works out the next part

The same with int()

int("21") + int("21")
1/3 ยท click the arrow - Python works out the next part

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. ๐ŸŽ‚

1๏ธโƒฃ
โŒจ๏ธ age_str?
โžก๏ธ
โš™๏ธint()
โžก๏ธ
๐Ÿ“ฆ age?

Type a number on the screen and press Enter - input() gives it as text.

Code
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)
Screen
โ–
1/2 ยท line 1 runs next
?

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(), then number = int(number_str).
  • Each number on its own line is a separate input() and a separate int().
  • A number with a dot: float(number_str) instead of int(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.