MASTER YOUR LOGIC BUILDING:Phase 1 - Level 2: Nested If & Multiple Conditions
Mastering phase 1 - level 2: nested if & multiple conditions concepts and implementation.
Phase 1 - Level 2: Nested If & Multiple Conditions
Why This Matters
At a theme park, the ride operator doesn't just ask your age — they also check your height. You need to be both old enough and tall enough to ride. Sometimes one condition isn't enough; you need to combine several checks.
That's what this level is about: asking multiple questions at once, or asking a follow-up question only after the first answer is yes.
What You'll Learn
- How to combine conditions with
and,or, andnot - How to nest one
ifinside another - When to use
elifinstead of nesting - How to solve problems like grade calculation and shape validation
Combining Conditions
AND — both must be true
if age >= 18 and height >= 140:
print("You can ride!")
Think: "I need this AND that."
OR — at least one must be true
if day == "Saturday" or day == "Sunday":
print("It's the weekend!")
Think: "Either this OR that is fine."
NOT — flip the answer
if not is_raining:
print("Let's go outside!")
Think: "Do the opposite of what this says."
Nested If — A Question Inside a Question
Sometimes you check one thing first, then ask a follow-up:
if sides_form_valid_triangle:
if all_sides_equal:
print("Equilateral")
elif two_sides_equal:
print("Isosceles")
else:
print("Scalene")
else:
print("Not a valid triangle")
Step-by-step for triangle type:
- First ask: "Can these three sides even make a triangle?"
- Only if yes, ask: "Are all sides equal? Two equal? Or all different?"
- Handle the invalid case in
else
elif — Cleaner Than Deep Nesting
When you're picking one outcome from several (like grades), use elif:
if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
else:
print("Grade: F")
Python checks from top to bottom and stops at the first match. That's why we check 90 before 80 — order matters!
Common Mistakes
- Deep nesting — more than 2–3 levels gets hard to read; try
eliforandinstead - Wrong operator — "18 or older AND student ID" needs
and, notor - Checking grades in wrong order — always check highest marks first when using
elif - Forgetting parentheses in complex conditions — use them to make intent clear:
(a > 0) and (b > 0)
Tips
- Write each condition as a plain-English sentence first, then translate to code
- For "pick one category" problems → use
if / elif / else - For "must pass all rules" problems → use
and - For "any one rule is enough" problems → use
or
Practice Focus
Try the examples below on your own. Pay special attention to:
- Checking validity first, then type (triangle problem)
- Using an
elifchain where order matters (grade calculator) - Combining multiple conditions with
andandor
Hands-on Examples
Check Triangle Type
# Take three sides as input
a = float(input("Enter side 1: "))
b = float(input("Enter side 2: "))
c = float(input("Enter side 3: "))
# Check if valid triangle
if a + b > c and b + c > a and c + a > b:
# Valid triangle - determine type
if a == b == c:
print("Equilateral")
elif a == b or b == c or c == a:
print("Isosceles")
else:
print("Scalene")
else:
print("Invalid triangle")First check if sides form a valid triangle (sum of any two sides > third side). Then check if all sides equal (equilateral), two sides equal (isosceles), or all different (scalene).
Related Page
🔗Related Content
- 💻
Phase 1 - Practice Problems
Practice Phase 1 concepts with hands-on coding problems
- 📝
Phase 1 - Quiz
Test your Phase 1 understanding with assessment questions
- ➡️
Phase 2 - Get Started
Continue to Phase 2 after mastering Phase 1
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum