Session 2 of 5 · 60 minutes
Making Decisions
Compare values and make your program choose what to do with if, elif and else.
Goals
By the end of this session you can:
- Compare values with ==, !=, <, >, <= and >=
- Write if, elif and else blocks and indent them correctly
- Combine conditions with and, or and not
- Use % to check whether a number is even or odd
Words to know
- condition
- A question with a True or False answer, like age >= 13.
- boolean
- A value that is either True or False.
- indentation
- The spaces at the start of a line. In Python they show which lines belong inside an if.
- modulo (%)
- The remainder after dividing. 7 % 2 is 1.
The lesson
Step 1: Asking yes/no questions
A comparison asks a question about two values. The answer is always True or False.
print(7 > 3)
print(7 == 3)
print(7 != 3)
print(5 <= 5)True
False
True
True| Symbol | Meaning |
|---|---|
== | is equal to |
!= | is not equal to |
< and > | less than, greater than |
<= and >= | less than or equal, greater than or equal |
Be careful: = stores a value, and == compares two values. They are not the same.
Step 2: if and else
An if runs some code only when its condition is True. else runs when it is not.
temperature = 31
if temperature > 30:
print("It's hot. Drink water!")
else:
print("Nice weather.")It's hot. Drink water!Two things to notice:
- The
ifline ends with a colon (:). - The lines that belong inside are indented by 4 spaces. That is how Python knows where the block starts and ends.
Step 3: More than two choices with elif
elif means “else if”. Python checks the conditions from top to bottom and runs the first one that is true.
score = 82
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Keep practicing!")Grade: BWhy the order matters: 82 is also greater than 70, but Python stops at the first true condition. If you wrote the
>= 70check first, everyone above 70 would get a C.
Step 4: and, or, not, and the remainder
Combine conditions with and, or and not.
age = 15
has_ticket = True
if age >= 13 and has_ticket:
print("Come in!")
if age < 5 or age > 100:
print("Please ask a grown-up.")
print(not has_ticket)Come in!
Falseandneeds both sides to be true.orneeds at least one side to be true.notflips True to False and False to True.
The % operator gives the remainder of a division. It is the standard way to test for even numbers.
print(7 % 2)
print(8 % 2)1
0If number % 2 == 0, the number divides evenly by 2, so it is even.
Step 5: Putting it together
Here is a short program that makes a decision from the user’s input.
age = int(input("How old are you? "))
if age >= 18:
print("You can vote in many countries.")
elif age >= 13:
print("You're a teenager.")
else:
print("You're still a kid. Enjoy it!")How old are you? 15
You're a teenager.Tip: When your program does the wrong thing, test it with the “edge” values: the numbers right on either side of each rule, like 12 and 13.
Exercise
Even or odd?
- Ask the user for a whole number and convert it with
int(). - Use
%to check whether the number divides by 2 with no remainder. - Print
evenoroddaccordingly. - Extra: also print
That's a big number!if the number is bigger than 100.
Need a hint?
A number is even when number % 2 == 0.
Small project
Ticket price calculator
A theater charges by age. Write a program that asks for the visitor's age and prints their ticket price.
- Ages 0 to 4 get in free.
- Ages 5 to 12 pay $5.
- Ages 13 to 64 pay $10.
- Ages 65 and over pay $6.
- Print a friendly message such as
Your ticket costs $5.If the age is below 0, printThat doesn't look like a real age.
Stretch it: Ask whether the visitor has a student card and give a $2 discount to anyone aged 13 to 64 who answers yes.
Quiz
Check your understanding
Pick one answer for each question. Your score appears right in the page; nothing is sent anywhere.
Feedback on this lesson
Teachers and students: tell us what worked and what didn't. We read this to improve the curriculum.
Received
Thank you for the feedback.
The Education team reviews lesson feedback to improve the curriculum.
Preview mode: this form is not connected to a destination yet, so nothing was sent. Set its web address in src/data/config.json.