Interview process with nested if-else

EasyIf-Else Programs · 12 of 64

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.

Advertisement
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

  1. The outer if checks round 1.
  2. Only inside it does an inner if check round 2.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs