PART 1 β€’ LEVEL 04

πŸ”€ Decision Making

Teach your Python programs to inspect conditions, choose the correct path and respond clearly to different situations.

⏱ 85–100 minutesπŸ“š 8 conceptsπŸ’» 5 programs🧠 6-question quiz
result.pyPython 3
score = 72

if score >= 75:
    grade = "Distinction"
elif score >= 60:
    grade = "Good"
else:
    grade = "Keep Practising"

print(grade)
OUTPUTGood

By the End of This Level

You will be able to design readable and complete decision paths.

01

Write if, if...else and if...elif...else statements.

02

Combine comparisons into meaningful conditions.

03

Use nesting and conditional expressions carefully.

04

Choose between conditional chains and match...case.

01

Conditions and Control Flow

A condition is an expression that Python treats as true or false. Control flow decides which statement runs next.

Python evaluates the condition after if. When it is True, the indented block runs. When it is False, Python skips that block and continues with the next available path.

Decision

A point where program execution follows one path and skips another according to a condition.

1Read DataGet current values
β†’
2Test ConditionTrue or False
β†’
3Choose BlockRun one path
β†’
4ContinueResume the program
02

The if Statement

Use a simple if when an action is needed only for the true case.

EXAMPLE 1

Free Delivery Check

order_total = 850

if order_total >= 500:
    print("Free delivery unlocked")

print("Order checked")
OUTPUT
Free delivery unlocked
Order checked
Indentation creates the block

The colon begins the decision block. Every statement controlled by that if must use consistent indentationβ€”normally four spaces.

03

if...else

Use else when exactly one of two mutually exclusive actions must run.

EXAMPLE 2

Even or Odd

number = 27

if number % 2 == 0:
    print("Even")
else:
    print("Odd")
OUTPUT
Odd
TRUE PATH
if balance >= price:
    print("Purchase approved")

Runs only when the condition is true.

FALSE PATH
else:
    print("Insufficient balance")

Runs only when the preceding condition is false.

04

if...elif...else

An elif chain checks conditions from top to bottom and runs only the first matching block.

EXAMPLE 3

Performance Band

score = 86

if score >= 90:
    band = "Excellent"
elif score >= 75:
    band = "Very Good"
elif score >= 60:
    band = "Good"
else:
    band = "Needs Improvement"

print(band)
OUTPUT
Very Good
Order matters

Place the most restrictive or highest range first. If score >= 60 appeared before score >= 90, a score of 95 would stop at the wrong branch.

05

Nested and Combined Conditions

NESTED

Use an inner decision

Useful when the second test matters only after the first succeeds.

if account_active:
    if balance >= price:
        print("Approved")
READABILITY

Name complex conditions

A descriptive Boolean variable explains the rule.

can_apply = age >= 18 and score >= 60
if can_apply:
    print("Eligible")
06

Conditional Expression

A conditional expression chooses between two values. Keep it for short, simple assignments.

EXAMPLE 4

Access Label

age = 20
label = "Adult" if age >= 18 else "Minor"
print(label)
OUTPUT
Adult
Read it in this order

true_value if condition else false_value. Use a normal statement when the branches perform several actions or the one-line form becomes difficult to scan.

07

match...case

Python 3.10+ can match one value or pattern against several cases. The underscore _ acts as the default case.

EXAMPLE 5

Menu Command

command = "save"

match command:
    case "open":
        print("Opening file")
    case "save":
        print("Saving file")
    case "quit":
        print("Closing program")
    case _:
        print("Unknown command")
OUTPUT
Saving file
SituationPreferReason
Ranges such as marks or ageif...elifComparisons express ranges clearly.
Exact commands or structured patternsmatch...caseCases keep discrete choices readable.
Only two returned valuesConditional expressionConcise when the rule is simple.

Common Decision-Making Mistakes

Comparison
if score = 50:

Use == to compare; = is assignment.

Indentation
print("Approved")

Indent every controlled statement consistently under its decision header.

Ordering
if score >= 40

Check higher thresholds before lower ones in an ordered elif chain.

Coverage
if number > 0

Include zero and negative paths when the problem requires all possibilities.

Debug decisions:1. List every possible path2. Test boundary values3. Check branch order4. Verify indentation5. Confirm exactly one result
QUICK REVISION

πŸ“Œ Decision Making β€” Quick Revision

Review these rules before attempting the quiz and programming problems.

01

A condition is interpreted as True or False.

02

if runs its block only for a true condition.

03

else handles the remaining false path.

04

elif adds ordered alternatives.

05

Only the first matching block in an elif chain runs.

06

Four consistent spaces are the standard indentation.

07

Use and, or and not to combine rules.

08

Boundary values reveal missing or overlapping ranges.

09

A conditional expression chooses between two values.

10

match...case suits clear discrete patterns.

Level 4 Quick Quiz

Select one answer for every question, then check your score.

6 Questions
1Which symbol finishes an if header?
2What does an elif chain execute?
3What is printed for number = 0 and if number:?
4Which condition checks whether age is from 18 through 60 inclusive?
5Which is a valid conditional expression?
6Which match pattern is commonly used as the default?

PRACTICE

🎯 5 Programming Problems β€” Decision Making

Solve every problem in the CodeBhavya pattern: understand the rule, inspect the sample, try the editor, run, check, then compare with the official program.

πŸ“ˆ Decision-Making Practice Progress
Solved0 / 5
Completed with Solution0
Total Score0 / 500
Completion0%

A problem counts as Solved when its output passes before the official solution is opened.

1. Number Sign Classifier

Read an integer and display whether it is positive, negative or zero.

Sample input: 0Expected output: Zero
2. Even or Odd

Read an integer and display whether it is even or odd.

Sample input: 27Expected output: Odd
3. Grade Classifier

Assign A for 90+, B for 75–89, C for 60–74, D for 40–59 and F otherwise.

Sample input: 86Expected output: Grade = B
4. Largest of Three

Read three integers and display the largest value.

Sample input: 18, 42, 31Expected output: Largest = 42
5. Placement Eligibility

A learner is eligible when age is at least 18, percentage is at least 60 and backlogs are zero.

Sample input: 20, 72, 0Expected output: Eligible
KEY TAKEAWAY

🎯 Make Every Path Complete

1List

Identify every possible outcome.

β†’
2Order

Place conditions from specific to general.

β†’
3Choose

Run exactly the intended branch.

β†’
4Test

Verify boundaries and exceptions.

Reliable decisions come from complete paths, correct ordering and boundary testsβ€”not from adding more conditions than the problem needs.

INTERACTIVE LEARNING

🎬 Decision Making β€” Visual Flow

Watch Python test score = 72, skip a false branch and select the first true branch.

PROGRAM TRACING

πŸ”Ž Program Tracing β€” Eligibility Decision

Move through one statement at a time and observe the condition, chosen branch, variables and output.

INTERVIEW PREPARATION

🎀 Decision Making β€” Interview Questions

Answer aloud before opening each explanation.

1.

What is the difference between independent if statements and an elif chain?

2.

Why does indentation matter in Python decisions?

3.

Why should higher thresholds appear first in a grade chain?

4.

When is nesting preferable to one combined condition?

5.

What are truthy and falsy values?

6.

When should match...case be considered?

EXTRA TIPS

πŸ’‘ Decision Making β€” Extra Tips

  • 01

    Test the values immediately below, at and above every boundary.

  • 02

    Use descriptive Boolean names such as is_eligible and has_access.

  • 03

    Keep decision blocks short; move repeated work after the chain.

  • 04

    Prefer a flat elif chain when deep nesting reduces readability.

  • 05

    Use in for a small group of accepted exact values.

  • 06

    Add an else only when a genuine default path exists.

EXTRA PRACTICE

✍️ Decision Making β€” Extra Practice Questions

  1. Read a year and decide whether it is a leap year.
  2. Read three side lengths and decide whether they can form a valid triangle.
  3. Classify a character as a vowel, consonant, digit or other symbol.
  4. Calculate an electricity-bill category from a given unit range.
  5. Display the smallest of four integers without using min().
  6. Check whether a date's month number belongs to a 30-day, 31-day or February group.
  7. Create a login decision using username, password and active-account status.
  8. Rewrite a two-branch assignment as a conditional expression.
  9. Build a menu with match...case for add, view, edit and quit commands.
  10. Trace a deliberately misordered grade chain, explain the bug and correct it.

Decision Making Complete

When you can design complete branches, handle boundary values and solve all five programs without help, mark this level complete and continue.