Lesson 31
Module 2 summary: Decisions
After this lesson: You'll go over the whole module and solve tasks that combine comparisons, ifs, and/or and the helpers.
Video
The video for this lesson is coming soon.
Explanation
Well done, this is the end of the second module! ๐ Your programs can already decide. In this lesson we'll go over everything, and at the end we'll build a real quiz show - with questions, points and a final score!
What you already know
| Lessons | Topic |
|---|---|
| 19, 20 | comparisons > < >= <= == !=, the values True and False |
| 21, 22, 23 | if, else and elif - one, two or many roads |
| 24 | divisibility: n % k == 0 |
| 25 | joining conditions: and, or, not |
| 26 | an if inside an if - a question behind a gate |
| 27 | comparing text |
| 28, 29 | guards and catching mistakes |
| 30 | a plan for a contest task |
1. Questions: comparisons and True / False
A comparison is a question, and the answer is True or False. One = puts into a box, two == ask. >= and <= count the limit too.
Recap: the scale
Click the arrow - we'll put every comparison on the scale.
1age = 122print(age >= 12)3print(age > 12)4print(age == 12)
โ2. Roads: if, else, elif
- if - a road only for True, else - for everything else, elif - another sign with a condition.
- Python checks from the top and picks the first true condition.
- The lines of each road are indented by 4 spaces, and every header ends with a colon.
Recap: the fork
points = 77print("Gold ๐ฅ")print("Silver ๐ฅ")print("Bronze ๐ฅ")print("Try again")Click the arrow - I'll walk the program's path, line by line.
1points = 772if points >= 90:3print("Gold ๐ฅ")4elif points >= 70:5print("Silver ๐ฅ")6elif points >= 50:7print("Bronze ๐ฅ")8else:9print("Try again")
โ3. Joining conditions
and - all the parts true (switches one after another), or - at least one (side by side), not - the other way round. Each side of and / or must be a whole question.
Recap: the circuit
Click the arrow - you'll see the condition as a circuit with switches and a bulb.
1n = 142if n % 2 == 0 and n > 10:3print("A big even number")4else:5print("Something else")
โ4. Guards and tests
- Check first, then calculate: a guard (an if) stands before a division and catches wrong data.
- Test the limits and every road of the program.
Bajtek's quiz show ๐ฌ
Click Next - the grand finale: a program that puts the whole module together.
Lights, camera, action!
Bajtek hosts a quiz show ๐ค. There are three questions. For every right answer the player gets a point, and at the end - a score.
Play the quiz show ๐ค
Three questions: what is 3 + 4? How many legs does a spider ๐ท๏ธ have? What is 12 // 4? Type three numbers, Enter โ after each - and see your prize!
1points = 02ans_str = input()3ans = int(ans_str)4if ans == 7:5points = points + 16ans_str = input()7ans = int(ans_str)8if ans == 8:9points = points + 110ans_str = input()11ans = int(ans_str)12if ans == 3:13points = points + 114print("Points:", points)15if points == 3:16print("๐ Champion!")17elif points == 2:18print("๐ฅ Great!")19elif points == 1:20print("๐ Not bad!")21else:22print("๐ช Try again!")
โWhat will this program print?
x = 6if x % 3 == 0 and x > 5:print("A")elif x > 5:print("B")else:print("C")
What will this program print?
a = 3b = 8wieksza = aif b > wieksza:wieksza = bprint(wieksza)
Which condition is true for Saturday (6) and Sunday (7) - and only for them?
Which if checks whether we can divide by b?
Remember
- A comparison gives True or False, and
==asks whether equal. - if / elif / else: Python picks the first true condition; a colon and indentation of 4 spaces.
- and - all of them, or - at least one, not - the other way round.
n % k == 0- divisibility; a tournament of ifs finds the biggest number.- The guard first, then the calculation. Test the limits and every road.
What's next?
In the next module programs will learn to repeat - the while and for loops. Then the quiz show can have not three but a hundred questions! ๐
That's all of Module 2! Now the tasks - each one combines a few topics. Good luck!
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.