🐍 Python for kids · Class 6 · Lesson 13 of 18 · Free
Nested If – Decisions Inside Decisions
⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Write an if statement inside another if or else block.
- Use indentation to show which question belongs to which answer.
- Decide when to use a nested if and when elif or and is cleaner.
At the cinema hall, the guard first asks: do you have a ticket? Only if you say yes does he ask the next question: is it for this show? That is a decision inside a decision. Python calls it a nested if.
What is a nested if in Python?
A nested if is an if statement that sits inside another if, elif or else block.
The inner question is asked only when the outer answer allows it. If the outer check fails, Python skips the whole inner part.
Indentation shows the nesting. The inner if is pushed 4 more spaces to the right.
▶has_ticket = True2show = "6 pm"3if has_ticket:4 print("Ticket checked.")5 if show == "6 pm":6 print("Go to Screen 2.")7 else:8 print("Wrong show, sorry.")9else:10 print("Please buy a ticket first.")
No variables yet.
▌✏️ Change this code and run it yourself
Ticket checked. Go to Screen 2.
💡 Change has_ticket to False. The inner if is never even looked at.
How does Python read indentation in a nested if?
Python matches each else with the if at the same indentation. Spaces, not position on the page, decide who belongs to whom.
Below, the inner else is 4 spaces in, so it pairs with the inner if. The outer else is at the left edge, so it pairs with the outer if.
Use exactly 4 spaces for each level. Mixing 2 and 4 spaces confuses both Python and your reader.
▶marks = int(input("Your marks out of 100: "))2if marks >= 35:3 print("You passed.")4 if marks >= 90:5 print("Grade A+ – excellent!")6 else:7 print("Good, aim for 90 next time.")8else:9 print("Not passed. Let us practise together.")
No variables yet.
▌✏️ Change this code and run it yourself
Your marks out of 100: 93 You passed. Grade A+ – excellent!
Can I nest an if inside an else block?
Yes. Any block can hold another if, including else. This is useful when the second question only matters on the no side.
Think of a school bus. If it is a holiday, nobody goes. Otherwise, we ask a new question: is it raining?
Each extra level adds one more step of indentation.
Take an umbrella to school.
How do I check a username and password with nested if?
A login screen is a classic nested decision. First check the username. Only then is it worth checking the password.
This also gives clearer messages. The user learns exactly which part was wrong.
Real websites hide which part was wrong for safety. For learning, clear messages help.
▶user = input("Username: ")2if user == "riya":3 pin = input("PIN: ")4 if pin == "4321":5 print("Welcome back, Riya!")6 else:7 print("Wrong PIN.")8else:9 print("No such user.")
No variables yet.
▌✏️ Change this code and run it yourself
Username: riya PIN: 1111 Wrong PIN.
💡 The PIN is asked only for a known user. A nested if lets you skip work that is not needed.
Nested if or elif – which one should I use?
elif picks one answer from a list of choices about the same thing, like marks bands.
A nested if asks a different, follow-up question, like age and then ticket type.
If you can join two checks with and, that is often shorter than nesting. Both programs below print the same thing.
Nested: you may enter the coding fest. With and: you may enter the coding fest.
💡 Keep nesting to two or three levels. Deeper code is hard to read, so look for and or elif instead.
How do I build a railway fare calculator with nested if?
Real programs mix nested if with elif. Here the outer check is the class of travel, and the inner check is age.
Children under 12 pay half fare in both classes. Notice how the same inner question repeats inside each branch.
Repeated inner code is a hint that a function could help, which you will learn soon.
Class (sleeper/ac): ac Age: 10 Your fare is Rs 600.
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
x = 15
if x > 10:
if x > 20:
print("big")
else:
print("medium")
else:
print("small")Question 2. What does this print?
rain = False
wind = True
if rain:
print("umbrella")
if wind:
print("jacket")
print("go")Question 3. What does this print?
n = 8
if n % 2 == 0:
if n > 5:
print("even and big")
print("even")
else:
print("odd")🧩 Your challenge
Write a mobile data helper. Ask if the user has a recharge (yes/no). If yes, ask how many GB are left. Print "Enjoy streaming!" for 2 GB or more, else "Data is low, save it." If no recharge, print "Please recharge first."
Do you have a recharge? (yes/no): yes GB left: 1.5 Data is low, save it.
⚠️ Common mistakes (and how to fix them)
❌ Wrong
age = 15
if age >= 13:
if age < 20:
print("Teenager")IndentationError: expected an indented block after 'if' statement on line 2
✅ Right
age = 15
if age >= 13:
if age < 20:
print("Teenager")The inner if must be indented under the outer if. Without spaces, Python finds an empty block and stops.
❌ Wrong
marks = 20
if marks >= 35:
print("Pass")
if marks >= 90:
print("Top grade")
else:
print("Fail")No error message – but marks 20 prints nothing, and marks 50 prints Pass and then Fail.
✅ Right
marks = 20
if marks >= 35:
print("Pass")
if marks >= 90:
print("Top grade")
else:
print("Fail")The else was indented with the inner if, so it paired with marks >= 90. Move it left to pair with the outer if.
📌 Remember
- A nested if is an if inside another if, elif or else block.
- The inner check runs only when the outer block runs.
- Each else pairs with the if at the same indentation.
- Use 4 spaces for every level and keep nesting shallow.
- When two checks just need to be both True, and is often shorter than nesting.
Finished the quiz and the challenge?
Questions students ask
How many levels can I nest if statements in Python?
Python allows many levels, but two or three is a good limit. Deeper code is hard to read and easy to break.
Is a nested if the same as elif?
No. elif chooses one option about the same question. A nested if asks a new follow-up question inside one answer.
Why does my else attach to the wrong if?
Python matches else to the if at the same indentation. Line up the else exactly under the if you mean.
Can I put a loop inside an if block?
Yes. Any block can hold loops, other ifs or prints. Just keep the indentation correct.