Lesson 5 of 12
Learn if/else with the Decision block: compare values, check two things with and/or, and pick one of many paths with Choose. Try each and see the code.
About 7 minutes · 4 parts, each with a program to try
Part 1 of 4
A Decision asks a true-or-false question, like age >= 18. If it's true, the program follows the Yes arrow; if not, the No arrow.
In code this is an if / else.
ageage >= 18 true?Yes →
"You can vote!"No →
"Not yet!"20It showsYou can vote!15It showsNot yet!age = int(input("How old are you? "))if age >= 18: print("You can vote!")else: print("Not yet!")Part 2 of 4
== means is equal to (two equals signs), != means is not. > and < mean more and less; >= and <= include equal.
Text must match exactly, capitals and all: "Dragon" is not "dragon".
passwordpassword == "dragon" true?Yes →
"Gate opens!"No →
"Wrong password!"dragonIt showsGate opens!catIt showsWrong password!password = input("What is the password? ")if password == "dragon": print("Gate opens!")else: print("Wrong password!")Part 3 of 4
&& (and) needs both to be true: age >= 13 && age <= 19 is a teenager.
|| (or) needs just one: day == "Sat" || day == "Sun" is the weekend.
ageage >= 13 && age <= 19 true?Yes →
"You are a teenager."No →
"You are not a teenager."15It showsYou are a teenager.21It showsYou are not a teenager.age = int(input("How old are you? "))if age >= 13 and age <= 19: print("You are a teenager.")else: print("You are not a teenager.")Part 4 of 4
When one value has several possible answers, like a menu, Choose is tidier than a stack of Decisions.
It follows the first case that matches, or other when none does.
choicechoiceWhen it is 1 →
"Tea"When it is 2 →
"Coffee"When it is 3 →
"Juice"Otherwise →
"We only have 1, 2 or 3"2It showsCoffee7It showsWe only have 1, 2 or 3choice = int(input("Pick 1, 2 or 3 "))if choice == 1: print("Tea")elif choice == 2: print("Coffee")elif choice == 3: print("Juice")else: print("We only have 1, 2 or 3")Quick check