Nested if-else Statements
Class Notes & Homework
1. What is a Nested if-else?
A nested if statement means placing one if (or if...else) statement INSIDE another one. The inner
condition is only checked if the outer condition is already True — it lets us ask a more specific
question after a general one has already been answered.
Real-life analogy:
"IF it is a weekday, THEN check IF it is before 9am — if yes, you are on time; if not, you
are late. IF it is not a weekday, then it is the weekend, no work today." The second
question ("before 9am?") only makes sense to ask AFTER we already know it is a
weekday.
2. Syntax
if condition1:
if condition2:
# runs only if BOTH condition1 and condition2 are True
else:
# runs if condition1 is True but condition2 is False
else:
# runs if condition1 is False
3. Indentation Rules (Extra Important Here!)
This is the number one place beginners make mistakes, so slow down here:
• Every time you go one level deeper (an if inside an if), you add one more indentation step —
usually 4 more spaces.
• The else for an inner if must line up EXACTLY under its own if — not the outer one.
• Mixing up indentation levels will not just look messy — it will completely change which if an
else belongs to, or cause an IndentationError.
if age >= 18: # level 0
if has_id: # level 1 (4 spaces)
print("Entry allowed") # level 2 (8 spaces)
else: # level 1 (lines up with its own if)
print("ID required") # level 2 (8 spaces)
else: # level 0 (lines up with the FIRST if)
print("Too young to enter") # level 1 (4 spaces)
Tip:
Count the ifs above a line to know how many indent levels it needs. Two ifs above you
means indent twice.
4. Today's Lab Task: Login System
This is the program we built together in class. It first checks the username, and only if that is
correct does it go on to ask for and check the password — a perfect example of nested if-else.
username = input("Enter username: ")
if username == "admin":
password = input("Enter password: ")
if password == "12345":
print("Login successful")
else:
print("Incorrect password")
else:
print("Username not found")
Notice:
The password is only ever asked for if the username is correct. This is exactly why we use
NESTING — the second check only makes sense after the first one has already passed.
Homework Tasks
Practice writing your own nested if-else programs. No solutions given — try
it yourself!
Task 1: Movie Ticket Entry
Ask the user for their age. If they are 13 or older, ask if they have a ticket. If they have a ticket,
print a welcome message. If not, print that they need a ticket first. If they are under 13, print that
the movie is 13+.
Task 2: Largest of Three Numbers
Ask the user to enter three numbers. Using nested if-else, find and print the largest of the three
numbers.
Task 3: Number Sign and Even/Odd
Ask the user for a number. First check if it is positive or negative. Then, inside each branch,
check whether the number is even or odd, and print both facts (for example: "Positive and
Even").
Task 4: Weather and Umbrella
Ask the user if it is raining (yes/no). If it is raining, ask if they have an umbrella. Print an
appropriate message for each combination of answers.
Task 5: Exam Result with Attendance
Ask the user for their marks. If marks are 50 or above, ask for their attendance percentage. If
attendance is 75% or above, print "Passed", otherwise print "Failed due to low attendance". If
marks are below 50, print "Failed due to low marks".
Task 6: Voting Eligibility with Registration
Ask the user for their age. If 18 or older, ask if they are registered to vote. Print whether they can
vote or need to register first. If under 18, print that they are not eligible yet.
Task 7: Discount Calculator
Ask the user for their total bill amount. If the bill is 1000 or more, ask if they are a member.
Members get 20% discount, non-members get 10%. If the bill is below 1000, print that no
discount is available.
Task 8: Temperature Checker
Ask the user for the temperature in Celsius. If it is above 30, ask if it is humid, and print a suitable
message either way. If it is 30 or below, check separately whether it is below 10 (cold) or
pleasant.
Task 9: Grading with Bonus Marks
Ask the user for their marks. If marks are 90 or above, ask if they submitted bonus work, and
print Grade A+ or Grade A accordingly. If marks are 70-89, print Grade B. Otherwise, print Grade
C or below.
Remember: think through your conditions first, then check your indentation carefully once you start writing
the code.