AlgoBazaAlgoBaza
Python for kids ยท Module 2

Lesson 28

Checking the data - before we calculate

After this lesson: You'll set up a guard: the program first checks the data and only then calculates - and doesn't crash.

Video

?

The video for this lesson is coming soon.

Explanation

Remember how in lesson 13 the program crashed when dividing by zero? Now that you know ifs, you can set up a guard ๐Ÿ›ก๏ธ: first check whether the data makes sense, and only then calculate. Such a program won't crash, even when someone types something strange!

The guard at the division ๐ŸŽฌ

Click Next - an if can save the program from an error.

Bajtek
๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌรท0โžก๏ธ๐Ÿ’ฅ

Code

sweets = 6
children = 0
print(sweets // children)

๐Ÿ–ฅ๏ธ Screen

ZeroDivisionError: integer division or modulo by zero
๐Ÿ’ฅ the program crashed
1

Sweets for nobody

The program shares sweets ๐Ÿฌ between children: sweets // children. But today nobody came - children = 0. Python tries to divide by zero and... ZeroDivisionError! The program crashes.

Step 1 / 4

Without a guard

Dividing by zero - the program crashes on the last line.

6 to share
๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ๐Ÿฌ
๐Ÿช‘ ๐Ÿช‘ ๐Ÿช‘0 children - nobody to give them to!

Click the arrow. In a moment Python tries to divide 6 by 0.

Code
1sweets = 6
2children = 0
3print(sweets // children)
Screen
โ–
1/4 ยท line 1 runs next

With a guard

The guard checks for zero before we get to the division - the program ends calmly.

๐Ÿ›ก๏ธ1sweets = 6
2children = 0
if children == 0
4print("Nobody to give them to ๐Ÿฌ")
โœ‹
elsein every other case
6print(sweets // children)
๐Ÿฌ

Click the arrow - I'll walk the program's path, line by line.

Code
1sweets = 6
2children = 0
3if children == 0:
4 print("Nobody to give them to ๐Ÿฌ")
5else:
6 print(sweets // children)
Screen
โ–
1/5 ยท line 1 runs next

Sharing sweets safely ๐Ÿฌ

Type the number of sweets ๐Ÿฌ, then the number of children ๐Ÿง’, Enter โŽ after each. Try 0 children too - the program won't crash!

๐Ÿ›ก๏ธ1sweets_str = input()
2sweets = int(sweets_str)
3children_str = input()
4children = int(children_str)
if children == 0
6print("Nobody to give them to ๐Ÿฌ")
โœ‹
elsein every other case
8print("Each gets:", sweets // children)
9print("Left over:", sweets % children)
๐Ÿฌ

Click the arrow - I'll walk the program's path, line by line.

Code
1sweets_str = input()
2sweets = int(sweets_str)
3children_str = input()
4children = int(children_str)
5if children == 0:
6 print("Nobody to give them to ๐Ÿฌ")
7else:
8 print("Each gets:", sweets // children)
9 print("Left over:", sweets % children)
Screen
โ–
1/2 ยท line 1 runs next

What can go wrong?

A guard is useful not only for zero. The data can also be nonsense - the program won't crash, but it will print silly things:

DataProblemGuard
children = 0dividing by zeroif children == 0:
age = -5an age can't be negativeif age < 0:
month = 13there are only 12 monthsif month < 1 or month > 12:
a withdrawal bigger than the money in the bankit can't be paid outif withdrawal > balance:

A clever trick: the guard checks whether the data is wrong (with or), and all the calculating sits in the else.

Bajtek's cash machine ๐ŸŽฌ

Click Next - the guard makes sure no more leaves the account than there is in it.

Bajtek
๐Ÿฆ50 zloty-๐Ÿ’ธ80 zloty=-30
1

50 zloty in the account

Ola has 50 zloty in the bank ๐Ÿฆ. Ola wants to withdraw 80 zloty for a bike. Without a guard the program would do 50 - 80 = -30 - and the bank would be surprised!

Step 1 / 3

The cash machine ๐Ÿง

The account holds 50 zloty. Type the amount to withdraw ๐Ÿ’ธ and press Enter โŽ - try more and less than 50!

๐Ÿง1balance = 50
2withdrawal_str = input()
3withdrawal = int(withdrawal_str)
if withdrawal > balance
5print("Not enough money ๐Ÿ›‘")
๐Ÿ›‘
elsein every other case
7balance = balance - withdrawal
8print("Paid out! Left:", balance)
๐Ÿ’ฐ

Click the arrow - I'll walk the program's path, line by line.

Code
1balance = 50
2withdrawal_str = input()
3withdrawal = int(withdrawal_str)
4if withdrawal > balance:
5 print("Not enough money ๐Ÿ›‘")
6else:
7 balance = balance - withdrawal
8 print("Paid out! Left:", balance)
Screen
โ–
1/3 ยท line 1 runs next

Which month is it? ๐Ÿ“†

Type a month number ๐Ÿ“† and press Enter โŽ. Try 13 or 0 too - the guard will stop them!

๐Ÿ“†1month_str = input()
2month = int(month_str)
if month < 1 or month > 12
4print("There is no such month!")
๐Ÿ›‘
elif month <= 2 or month == 12
6print("Winter โ„๏ธ")
โ„๏ธ
elif month <= 5
8print("Spring ๐ŸŒท")
๐ŸŒท
elif month <= 8
10print("Summer โ˜€๏ธ")
โ˜€๏ธ
elsein every other case
12print("Autumn ๐Ÿ‚")
๐Ÿ‚

Click the arrow - I'll walk the program's path, line by line.

Code
1month_str = input()
2month = int(month_str)
3if month < 1 or month > 12:
4 print("There is no such month!")
5elif month <= 2 or month == 12:
6 print("Winter โ„๏ธ")
7elif month <= 5:
8 print("Spring ๐ŸŒท")
9elif month <= 8:
10 print("Summer โ˜€๏ธ")
11else:
12 print("Autumn ๐Ÿ‚")
Screen
โ–
1/2 ยท line 1 runs next
?

Where should the guard (an if) checking for zero stand?

?

Which condition catches a wrong month number?

Remember

  • Check first, then calculate - the guard (an if) stands before the dangerous spot.
  • Dividing by zero: if children == 0:, and the division itself in the else.
  • Nonsense data (a negative age, the 13th month) is worth stopping too.
  • Wrong data is often caught with or: if m < 1 or m > 12:.

On to the tasks! In each one first think: what data could break the program?

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.