Practice: Nested Conditions

« Read the chapter

Q1. Valid login

BeginnerCoding

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

BeginnerCoding

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

IntermediateCoding

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

IntermediateCoding

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

BeginnerCoding

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

AdvancedCoding

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

AdvancedCoding

Write a function sign(n) with early returns giving 'neg','zero','pos'. Print sign(-5).

Goal: Reduce nesting.

Q8. Both big

BeginnerOutput

What does this print?

Code
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

BeginnerOutput

What does this print?

Code
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

BeginnerOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
a = 3
if a > 1:
    if a > 5:
        print('X')
    else:
        print('Y')
print('Z')

Goal: Compare indentation effects.

Q13. Nested ladder

IntermediateOutput

What does this print?

Code
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

AdvancedOutput

What does this print?

Code
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

IntermediateDebugging

The program should print 'Wrong password' when the user is right but the password is wrong. Fix the indentation.

Code
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

BeginnerDebugging

This code gives an IndentationError. Fix it so it prints 'big'.

Code
n = 20
if n > 10:
if n > 15:
    print('big')

Goal: Fix indentation errors.

Q17. Fix the logic

AdvancedDebugging

The code should print 'teen' for age 15 but prints nothing. Fix it.

Code
age = 15
if age > 12:
    if age > 19:
        print('adult')
    elif age < 12:
        print('teen')

Goal: Spot impossible conditions.

Q18. Nest or flatten

IntermediateConceptual

When is it better to keep nested ifs instead of using and?

Goal: Choose between nesting and and.

Q19. Why avoid deep nesting

BeginnerConceptual

Give two reasons to avoid deep nesting.

Goal: Know code readability.

Q20. Entry rules

IntermediateApplication

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.