MASTER YOUR LOGIC BUILDING:Phase 6 - Category 5: Applied Problems

Mastering phase 6 - category 5: applied problems concepts and implementation.

Applied Problems

Why This Matters

This is where programming meets the real world. Grading students, validating passwords, categorizing people by age, building a simple calculator — these aren't abstract puzzles. They're the kind of problems developers solve every day. Phase 6 ends here because you're ready to take everything you've learned and apply it to practical scenarios.

What You'll Learn

  • How to turn a real-world description into code logic
  • Three reusable patterns: validation, categorization, and statistics
  • How to combine loops, conditions, and data structures in one program
  • How to test your solution with realistic inputs

Pattern 1: Validation (Pass or Fail)

"Does this password meet all the rules?"

List every rule as a separate check, then combine with and:

password = input("Enter password: ")

has_min_length = len(password) >= 8
has_digit = any(char.isdigit() for char in password)
has_letter = any(char.isalpha() for char in password)

if has_min_length and has_digit and has_letter:
    print("Valid password!")
else:
    print("Invalid — needs 8+ chars, at least one letter and one digit")

Each rule is one boolean. Easy to read, easy to debug.

Pattern 2: Categorization (Which Bucket?)

"Classify age into child, teen, adult, or senior."

Use if / elif / else like a decision tree:

age = int(input("Enter age: "))

if age < 13:
    category = "Child"
elif age < 20:
    category = "Teen"
elif age < 60:
    category = "Adult"
else:
    category = "Senior"

print(f"Category: {category}")

Draw the thresholds on a number line first — it prevents gaps and overlaps.

Pattern 3: Statistics (Count, Sum, Average)

"How many students passed? What's the average score?"

marks = [45, 35, 50, 38, 42]
passed = 0
total = 0

for mark in marks:
    total += mark
    if mark >= 40:
        passed += 1

average = total / len(marks)
print(f"Passed: {passed}, Average: {average}")

One loop can count and sum at the same time — efficient and clean.

Step-by-Step: From Words to Code

When you read an applied problem:

  1. Identify inputs — what does the user provide?
  2. Identify outputs — what should the program print or return?
  3. List rules — write each condition in plain English
  4. Pick a pattern — validation? categorization? statistics?
  5. Code one rule at a time — test each before adding the next
  6. Run test cases — normal input, boundary input, invalid input

A Mini Calculator Example

a = float(input("First number: "))
b = float(input("Second number: "))
op = input("Operator (+, -, *, /): ")

if op == '+':
    print(a + b)
elif op == '-':
    print(a - b)
elif op == '*':
    print(a * b)
elif op == '/':
    if b != 0:
        print(a / b)
    else:
        print("Cannot divide by zero")
else:
    print("Unknown operator")

Notice the nested check for division by zero — always guard against impossible operations.

Common Mistakes & Tips

  • Skipping the planning step — jumping straight to code leads to tangled logic
  • Missing edge cases — empty lists, zero division, boundary ages (exactly 13, exactly 60)
  • Hard-coding values — use variables so the program works for any valid input
  • Forgetting `else` — what happens when none of the conditions match?
  • Tip: Write 3 test cases on paper before coding: one normal, one edge, one that should fail

Practice Focus

  • Count students who scored 40 or above (pass/fail report)
  • Validate a password with at least 3 custom rules
  • Categorize ages or BMI values into health groups
  • Build a simple calculator with +, −, ×, ÷
  • Given a list of prices, find total, average, and count above a threshold

Congratulations — you've worked through the full Logic Building journey! These applied problems prove you can think like a programmer. Keep practicing, stay curious, and build something small every day. 🎉

Hands-on Examples

Students Passed (>=40)

# Take student marks
n = int(input("Enter number of students: "))
marks = []
for i in range(n):
    mark = int(input(f"Enter marks for student {i+1}: "))
    marks.append(mark)

# Count passed students (>=40)
passed = 0
for mark in marks:
    if mark >= 40:
        passed += 1

print(f"Students passed: {passed}")
print(f"Students failed: {n - passed}")

Iterate through marks array, count students with marks >= 40. Calculate failed as total minus passed.