Practice: Nested Conditions
Q1. Valid login
Set user='ravi', pw='1234'. If user is 'ravi' check pw is '1234' and print 'Welcome'; wrong pw prints 'Bad password'; wrong user prints 'No such user'.
Goal: Write nested if-else.
Q2. Positive even
Set n = 8. Print 'positive even' if n is positive and even, 'positive odd' if positive and odd, otherwise 'not positive'.
Goal: Nest a test inside a branch.
Q3. Ride eligibility
Height 140 and age 11. Ride needs height >= 120; if age under 12 a guardian is also needed. Print 'Guardian needed' in that case, 'Allowed' if height is enough and age is 12+, else 'Too short'.
Goal: Layer two rules.
Q4. Member discount
member = False, amount = 3000. Members get 20 off for 2000+, 10 otherwise; non-members get 5 off for 2000+, else 0. Print the discount.
Goal: Use nesting in both branches.
Q5. Flatten it
Rewrite: if a > 0 then if b > 0 print 'both'. With a=1, b=2, use one if with and.
Goal: Flatten nested conditions.
Q6. Triangle type
Sides 5, 5, 8. Print 'invalid' if the sum of any two sides is not greater than the third. Otherwise print 'equilateral', 'isosceles' or 'scalene'.
Goal: Combine and with a nested ladder.
Q7. Early return classify
Write a function sign(n) with early returns giving 'neg','zero','pos'. Print sign(-5).
Goal: Reduce nesting.
Q8. Both big
What does this print?
x = 9
y = 7
if x > 5:
if y > 5:
print('both')
else:
print('x only')
else:
print('none')Goal: Trace nested conditions.
Q9. Inner else
What does this print?
x = 9
y = 2
if x > 5:
if y > 5:
print('both')
else:
print('x only')
else:
print('none')Goal: Trace nested conditions.
Q10. Outer else
What does this print?
x = 1
y = 20
if x > 5:
if y > 5:
print('both')
else:
print('x only')
else:
print('none')Goal: Skip the inner block.
Q11. Indentation pairing
What does this print?
a = 3
if a > 1:
if a > 5:
print('X')
else:
print('Y')
print('Z')Goal: Pair else with its if.
Q12. Else pairs inner
What does this print?
a = 3
if a > 1:
if a > 5:
print('X')
else:
print('Y')
print('Z')Goal: Compare indentation effects.
Q13. Nested ladder
What does this print?
ok = True
n = 45
if ok:
if n > 90:
print('high')
elif n > 40:
print('mid')
else:
print('low')
else:
print('off')Goal: Trace nested ladders.
Q14. Flat and nested equal
What does this print?
a = 4
b = 0
if a > 0:
if b > 0:
print('both')
if a > 0 and b > 0:
print('both flat')
print('end')Goal: Compare nesting and and.
Q15. Fix the pairing
The program should print 'Wrong password' when the user is right but the password is wrong. Fix the indentation.
user = 'amy'
pw = 'bad'
if user == 'amy':
if pw == 'good':
print('Welcome')
else:
print('Wrong password')Goal: Fix else pairing.
Q16. Fix the missing block
This code gives an IndentationError. Fix it so it prints 'big'.
n = 20
if n > 10:
if n > 15:
print('big')Goal: Fix indentation errors.
Q17. Fix the logic
The code should print 'teen' for age 15 but prints nothing. Fix it.
age = 15
if age > 12:
if age > 19:
print('adult')
elif age < 12:
print('teen')Goal: Spot impossible conditions.
Q18. Nest or flatten
When is it better to keep nested ifs instead of using and?
Goal: Choose between nesting and and.
Q19. Why avoid deep nesting
Give two reasons to avoid deep nesting.
Goal: Know code readability.
Q20. Entry rules
Person age 17 with has_ticket True. Print 'Enter' if age is 18+ and ticket, 'Need adult' if under 18 with ticket, 'Buy ticket' if no ticket.
Goal: Model rules with nesting.