Python Practice › If-Else Programs › Program 12
Interview process with nested if-else
Write a Python program that describes a two-round interview: a candidate who passes round 1 can take round 2, and passing both means placed. Use nested if-else.
Input
("passed", "passed"), ("passed", "failed"), ("failed", "-")Output
--- round1=passed, round2=passed Round 1 cleared Round 2 cleared: you are placed --- round1=passed, round2=failed Round 1 cleared Failed in round 2, please try next time --- round1=failed, round2=- Failed in round 1, please try next time
Approach
- The outer
ifchecks round 1. - Only inside it does an inner
ifcheck round 2.
Solution
def interview(round1, round2): if round1 == "passed": print("Round 1 cleared") if round2 == "passed": print("Round 2 cleared: you are placed") else: print("Failed in round 2, please try next time") else: print("Failed in round 1, please try next time") for r1, r2 in [("passed", "passed"), ("passed", "failed"), ("failed", "-")]: print(f"--- round1={r1}, round2={r2}") interview(r1, r2)Output
--- round1=passed, round2=passed Round 1 cleared Round 2 cleared: you are placed --- round1=passed, round2=failed Round 1 cleared Failed in round 2, please try next time --- round1=failed, round2=- Failed in round 1, please try next time
Line by line
Line 2
The outer condition decides whether round 2 matters at all.
Lines 4-7
The inner if-else only runs for candidates who passed round 1.
Lines 8-9
A failed round 1 skips round 2 entirely.
Learn the concept
New to this topic? Read the lesson, then come back.