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.
--- 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 if checks round 1.
Only inside it does an inner if check round 2.
Solution
Python
definterview(round1,round2):ifround1=="passed":print("Round 1 cleared")ifround2=="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")forr1,r2in[("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.
Write a Python program to check whether a given number is prime.
Input
2, 15, 17, 1
Output
2 is prime
15 is not prime
17 is prime
1 is not prime
Approach
Numbers below 2 are not prime.
Try divisors from 2 to the square root; if one divides exactly, it is not prime.
Solution
Python
defis_prime(n):ifn<2:returnFalsefordinrange(2,int(n**0.5)+1):ifn%d==0:returnFalsereturnTruefornin[2,15,17,1]:ifis_prime(n):print(n,"is prime")else:print(n,"is not prime")
Output
2 is prime
15 is not prime
17 is prime
1 is not prime
Line by line
Lines 2-3
0 and 1 are not prime by definition.
Line 4
Checking divisors up to √n is enough.
Lines 5-6
Return early as soon as a divisor is found.
Learn the concept
New to this topic? Read the lesson, then come back.
Write a Python program to check whether a number is divisible by both 3 and 5.
Input
45, 30, 9, 10
Output
45 is divisible by 3 and 5
30 is divisible by 3 and 5
9 is divisible by 3 only
10 is divisible by 5 only
Approach
Combine the two remainder checks with and.
Solution
Python
fornin[45,30,9,10]:ifn%3==0andn%5==0:print(n,"is divisible by 3 and 5")elifn%3==0:print(n,"is divisible by 3 only")elifn%5==0:print(n,"is divisible by 5 only")
Output
45 is divisible by 3 and 5
30 is divisible by 3 and 5
9 is divisible by 3 only
10 is divisible by 5 only
Line by line
Line 2
and is true only when both conditions are true; this test must come first.
Lines 4-7
The other branches report partial matches.
Learn the concept
New to this topic? Read the lesson, then come back.