Nested if statements in Python

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Nested if statements in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. When is the inner if checked in a nested if statement?
A Every time the program runs
B Only when the outer condition is True
2. Which code shows a nested if?
A if age >= 18: if has_id: print("Enter")
B if age >= 18: if has_id: print("Enter")
3. score = 40 if score > 50: if score > 80: print("High") What does this code print?
A High
B Nothing
4. How do you show that an if statement is inside another if block?
A Indent it one extra level
B Put it in brackets ( )
5. Which statement about nested if and the and operator is correct?
A if a: followed by an inner if b: can act like if a and b:
B A nested if can never be written using and

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1if age >= 18: if has_id: print("Enter") AA nested if: the inner if has_id is only checked when the outer condition age >= 18 is True
2if score >= 50 and attempts <= 3: print("Pass") Btemp = 15 if temp > 20: if temp > 30: print("Hot")
3The inner if is indented one extra level (4 spaces) so Python knows it belongs inside the outer if block CA single if using and: the same result as a nested if, but both conditions are joined on one line
4If the outer condition is False, the inner if is skipped and never checked DThe inner if is not indented, so it is not inside the outer if block. Python raises an IndentationError because the outer block is empty.
5if age >= 18: if has_id: print("Enter") Eif logged_in: if is_admin: print("Admin panel")
6if x > 0: if x < 10 print("Small") FThe inner if is missing the colon at the end of its condition, which causes a SyntaxError.

Definitions

nested if

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
if age >= 18: __ has_licence: print("Can drive")
2.
if score >= 50: if score __ 90: print("Distinction")
3.
if age >= 18 ___ has_licence: print("Can drive")
4.
if x > 0: if x % 2 == 0: _____("Positive even")
5.
if mark >= 50: if mark __ 100: print("Perfect score")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A if logged_in == true:
B if score > 50:
C print('Pass')
Question 2
A iff mark >= 50:
B if mark >= 85:
C print('Top mark')
Question 3
A if age >= 18:
B if has_licence:
C print('Can drive'
Question 4
A if temp > 30:
B if humidity => 70:
C print('Humid')
Question 5
A if score >= 50:
B if score >= 80
C print('Merit')

Answer Key

Nested if statements in Python
Section A: Quick Questions
Q1
B — Only when the outer condition is True
Q2
A — if age >= 18: if has_id: print("Enter")
Q3
B — Nothing
Q4
A — Indent it one extra level
Q5
A — if a: followed by an inner if b: can act like if a and b:
Section B: Match the Pairs
Q1
if age >= 18: if has_id: print("Enter") → A nested if: the inner if has_id is only checked when the outer condition age >= 18 is True
Q2
if score >= 50 and attempts <= 3: print("Pass") → A single if using and: the same result as a nested if, but both conditions are joined on one line
Q3
The inner if is indented one extra level (4 spaces) so Python knows it belongs inside the outer if block → if logged_in: if is_admin: print("Admin panel")
Q4
If the outer condition is False, the inner if is skipped and never checked → temp = 15 if temp > 20: if temp > 30: print("Hot")
Q5
if age >= 18: if has_id: print("Enter") → The inner if is not indented, so it is not inside the outer if block. Python raises an IndentationError because the outer block is empty.
Q6
if x > 0: if x < 10 print("Small") → The inner if is missing the colon at the end of its condition, which causes a SyntaxError.
Section C: Definitions
Q1
nested if — An if statement indented inside another if block. The inner condition is only checked when the outer condition is True.
Section D: Fill in the Blanks
Q1
if age >= 18: if has_licence: print("Can drive")
Q2
if score >= 50: if score >= 90: print("Distinction")
Q3
if age >= 18 and has_licence: print("Can drive")
Q4
if x > 0: if x % 2 == 0: print("Positive even")
Q5
if mark >= 50: if mark == 100: print("Perfect score")
Section E: Spot the Error
Q1
A — Python's Boolean value is capitalised: True. Lowercase true is an undefined name, so the outer condition causes a NameError and the nested if never runs. It should be: if logged_in == True:
Q2
A — The keyword is misspelled. The outer statement must start with if, otherwise the nested if inside it has no valid outer block. It should be: if mark >= 50:
Q3
C — The print call is missing its closing bracket. It should be: print('Can drive'), which only runs when both the outer and inner conditions are True.
Q4
B — => is not a valid comparison operator in Python. The inner condition should use >=, or > as in the correct program: if humidity > 70:
Q5
B — The inner if statement is missing its colon. Every if line, including a nested one, must end with a colon: if score >= 80: