AlgoBazaAlgoBaza
Python for kids ยท Module 2

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

LessonsTopic
19, 20comparisons > < >= <= == !=, the values True and False
21, 22, 23if, else and elif - one, two or many roads
24divisibility: n % k == 0
25joining conditions: and, or, not
26an if inside an if - a question behind a gate
27comparing text
28, 29guards and catching mistakes
30a 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.

Code
1age = 12
2print(age >= 12)
3print(age > 12)
4print(age == 12)
Screen
โ–
1/5 ยท line 1 runs next

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

๐Ÿƒ1points = 77
if points >= 90
3print("Gold ๐Ÿฅ‡")
๐Ÿฅ‡
elif points >= 70
5print("Silver ๐Ÿฅˆ")
๐Ÿฅˆ
elif points >= 50
7print("Bronze ๐Ÿฅ‰")
๐Ÿฅ‰
elsein every other case
9print("Try again")
๐Ÿ’ช

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

Code
1points = 77
2if points >= 90:
3 print("Gold ๐Ÿฅ‡")
4elif points >= 70:
5 print("Silver ๐Ÿฅˆ")
6elif points >= 50:
7 print("Bronze ๐Ÿฅ‰")
8else:
9 print("Try again")
Screen
โ–
1/5 ยท line 1 runs next

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.

Code
1n = 14
2if n % 2 == 0 and n > 10:
3 print("A big even number")
4else:
5 print("Something else")
Screen
โ–
1/4 ยท line 1 runs next

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.

Bajtek
๐ŸŽคโ“โ“โ“โžก๏ธ๐Ÿ†
1

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.

Step 1 / 3

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!

๐Ÿ“ฆ points
โญโ€ฆ
Code
1points = 0
2ans_str = input()
3ans = int(ans_str)
4if ans == 7:
5 points = points + 1
6ans_str = input()
7ans = int(ans_str)
8if ans == 8:
9 points = points + 1
10ans_str = input()
11ans = int(ans_str)
12if ans == 3:
13 points = points + 1
14print("Points:", points)
15if points == 3:
16 print("๐Ÿ† Champion!")
17elif points == 2:
18 print("๐Ÿฅˆ Great!")
19elif points == 1:
20 print("๐Ÿ‘ Not bad!")
21else:
22 print("๐Ÿ’ช Try again!")
Screen
โ–
1/3 ยท line 1 runs next
?

What will this program print?

x = 6
if x % 3 == 0 and x > 5:
print("A")
elif x > 5:
print("B")
else:
print("C")
?

What will this program print?

a = 3
b = 8
wieksza = a
if b > wieksza:
wieksza = b
print(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.