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.
Code
๐ฅ๏ธ Screen
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.
Without a guard
Dividing by zero - the program crashes on the last line.
Click the arrow. In a moment Python tries to divide 6 by 0.
1sweets = 62children = 03print(sweets // children)
โWith a guard
The guard checks for zero before we get to the division - the program ends calmly.
sweets = 6children = 0print("Nobody to give them to ๐ฌ")print(sweets // children)Click the arrow - I'll walk the program's path, line by line.
1sweets = 62children = 03if children == 0:4print("Nobody to give them to ๐ฌ")5else:6print(sweets // children)
โ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!
sweets_str = input()sweets = int(sweets_str)children_str = input()children = int(children_str)print("Nobody to give them to ๐ฌ")print("Each gets:", sweets // children)print("Left over:", sweets % children)Click the arrow - I'll walk the program's path, line by line.
1sweets_str = input()2sweets = int(sweets_str)3children_str = input()4children = int(children_str)5if children == 0:6print("Nobody to give them to ๐ฌ")7else:8print("Each gets:", sweets // children)9print("Left over:", sweets % children)
โ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:
| Data | Problem | Guard |
|---|---|---|
| children = 0 | dividing by zero | if children == 0: |
| age = -5 | an age can't be negative | if age < 0: |
| month = 13 | there are only 12 months | if month < 1 or month > 12: |
| a withdrawal bigger than the money in the bank | it can't be paid out | if 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.
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!
The cash machine ๐ง
The account holds 50 zloty. Type the amount to withdraw ๐ธ and press Enter โ - try more and less than 50!
balance = 50withdrawal_str = input()withdrawal = int(withdrawal_str)print("Not enough money ๐")balance = balance - withdrawalprint("Paid out! Left:", balance)Click the arrow - I'll walk the program's path, line by line.
1balance = 502withdrawal_str = input()3withdrawal = int(withdrawal_str)4if withdrawal > balance:5print("Not enough money ๐")6else:7balance = balance - withdrawal8print("Paid out! Left:", balance)
โWhich month is it? ๐
Type a month number ๐ and press Enter โ. Try 13 or 0 too - the guard will stop them!
month_str = input()month = int(month_str)print("There is no such month!")print("Winter โ๏ธ")print("Spring ๐ท")print("Summer โ๏ธ")print("Autumn ๐")Click the arrow - I'll walk the program's path, line by line.
1month_str = input()2month = int(month_str)3if month < 1 or month > 12:4print("There is no such month!")5elif month <= 2 or month == 12:6print("Winter โ๏ธ")7elif month <= 5:8print("Spring ๐ท")9elif month <= 8:10print("Summer โ๏ธ")11else:12print("Autumn ๐")
โ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.