Stage 5 – Extend

Coding5s / Pillar 1 / Stage 05

Extend

Stage 5 tests transfer. The learner starts with an existing working solution, receives new requirements or constraints, and must adapt the implementation without losing the behavior that already works. The goal is to apply what was learned in a new direction rather than repeat the original task.

UNDERSTAND RECEIVE NEW REQUIREMENT ADAPT EXTEND VERIFY
Reference output: Below is a complete example of a Stage 5 lesson generated through the Coding5s prompt architecture. The new requirements, extension constraints, expected behavior, and implementation scope vary according to the technology, topic, learner level, and configured technical rules.

🔹 Topic #23 | Stage 5 | Python Core & Scripting | 📊 Level: Intermediate | 🌐 Coding5s.com

Concept: Conditional Branching (if, elif, else)

Dataset detected: NO. Generating dummy data.

1. Ticket Title

Implement Tiered Transaction Risk Scoring

2. Context & Ticket

🎯 Objective: Master feature implementation in a real-world environment. Golden Rule: Do not scroll down beyond the scrolling barrier until you have attempted your own solution. This is important because the complete solution, meaning Version 1.1 of the code, is located right after that barrier. Mastery does not come from copying code, but from the effort you make to reach it before looking at the solution. ⚠️ Warning. Copy the Version 1.0 code and paste it into your IDE, from there write the necessary lines to fulfill the ticket requirements and acceptance criteria.

🎫 Ticket #48291
👤 From: Sarah Jenkins – Risk & Compliance
📥 To: Dev Team

Subject: Implement Tiered Transaction Risk Scoring

Hello Dev Team, this is Sarah from Risk & Compliance. We need to implement a dynamic multi-way decision logic to categorize incoming financial transactions into risk tiers based on transaction amount, account age, and origin country.

(Tech Product Manager Notes):

👥 From: David Chen – Tech Product Manager
📥 To: Python Core & Scripting Dev

Subject: Assignment Details

Hello Python Core & Scripting Dev!

I’ve set up the basic script structure and the data ingestion pipeline. Your task is to build out the evaluate_risk() function using clean if, elif, and else statements. The branching logic must follow our new compliance rules strictly. Avoid deeply nested if-statements; use compound boolean expressions instead.

Acceptance Criteria:

  • If the transaction amount is greater than $10,000, return ‘High Risk’.
  • If the transaction amount is greater than $5,000 OR (amount > $1,000 AND country is NOT ‘US’), return ‘Medium Risk’.
  • If the transaction amount is greater than $100 AND the account is new (new_account is True), return ‘Low Risk’.
  • For all other cases, return ‘Minimal Risk’.

3. Expected Behavior

When a transaction is processed, the system must evaluate the conditions sequentially from top to bottom. Once a condition is met, it should immediately return the corresponding risk tier. Transactions failing all specific rules should safely fall back to “Minimal Risk”.

4. ⚠️ TRY BEFORE YOU SCROLL MESSAGE

Stop! Copy Version 1.0 below into your IDE. Try to implement the multi-way logic yourself using if, elif, and else before looking at the solution.

5. Support Files

(Not required for this specific integration. Only pure Python logic is needed.)

6. 🌱 Base Code (V1.0)

Python
def evaluate_risk(transaction):
    amount = transaction.get("amount", 0.0)
    country = transaction.get("country", "US")
    new_account = transaction.get("new_account", False)
    
    risk_level = "Unassessed"
    
    # TODO: Implement multi-way conditional decision paths (if, elif, else)
    # based on the acceptance criteria in Ticket #48291.
    
    return risk_level

def process_transactions(data):
    results = []
    for txn in data:
        risk = evaluate_risk(txn)
        results.append((txn["id"], risk))
        print(f"Txn {txn['id']} | Amount: ${txn.get('amount', 0):>9,.2f} | Risk: {risk}")
    return results

transactions = [
    {"id": "T001", "amount": 12500.00, "country": "US", "new_account": False},
    {"id": "T002", "amount": 6000.00, "country": "UK", "new_account": True},
    {"id": "T003", "amount": 1500.00, "country": "JP", "new_account": False},
    {"id": "T004", "amount": 800.00, "country": "US", "new_account": True},
    {"id": "T005", "amount": 50.00, "country": "US", "new_account": False}
]

process_transactions(transactions)

7. Version 1.0 Console Output

Plaintext
Txn T001 | Amount: $12,500.00 | Risk: Unassessed
Txn T002 | Amount: $ 6,000.00 | Risk: Unassessed
Txn T003 | Amount: $ 1,500.00 | Risk: Unassessed
Txn T004 | Amount: $   800.00 | Risk: Unassessed
Txn T005 | Amount: $    50.00 | Risk: Unassessed

8. 🛠️ YOUR TURN MESSAGE

You have the requirements and the skeleton code. Your turn! Add the if/elif/else statements directly into the evaluate_risk function. Ensure your comparisons check the largest thresholds first!

9. 💡 Implementation Hint

Always start with your most restrictive or highest priority condition (> 10000). Remember that Python will short-circuit the evaluation as soon as it hits a True statement. Use the or and and keywords to combine conditions smoothly within a single elif block.

10. ⚠️ SOLUTION BARRIER BELOW

🛑 STOP BARRIER 🛑

11. 🌳 Updated Code (V1.1 – FULLY IMPLEMENTED)

Python
def evaluate_risk(transaction):
    # Ensure data robustness by explicitly casting types
    amount = float(transaction.get("amount", 0.0))
    country = str(transaction.get("country", "US")).upper()
    new_account = bool(transaction.get("new_account", False))
    
    # Implement multi-way conditional decision paths
    if amount > 10000.00:
        risk_level = "High Risk"
    elif amount > 5000.00 or (amount > 1000.00 and country != "US"):
        risk_level = "Medium Risk"
    elif amount > 100.00 and new_account:
        risk_level = "Low Risk"
    else:
        # The 'else' block acts as a safe fallback catching everything else
        risk_level = "Minimal Risk"
        
    return risk_level

def process_transactions(data):
    results = []
    for txn in data:
        risk = evaluate_risk(txn)
        results.append((txn["id"], risk))
        print(f"Txn {txn['id']} | Amount: ${txn.get('amount', 0):>9,.2f} | Risk: {risk}")
    return results

transactions = [
    {"id": "T001", "amount": 12500.00, "country": "US", "new_account": False},
    {"id": "T002", "amount": 6000.00, "country": "UK", "new_account": True},
    {"id": "T003", "amount": 1500.00, "country": "JP", "new_account": False},
    {"id": "T004", "amount": 800.00, "country": "US", "new_account": True},
    {"id": "T005", "amount": 50.00, "country": "US", "new_account": False}
]

process_transactions(transactions)

12. 📝 Implementation Notes

  • Sequential Evaluation: The if/elif/else chain evaluates from top to bottom. T001 ($12,500) triggers the if block immediately and skips the rest of the branch.
  • Compound Conditions: In the first elif, we combined rules using or and and. This allows T002 to match via amount > 5000.00, while T003 matches because it’s over $1000 and not from the US.
  • Robustness: Before evaluating, values are safely extracted using dictionary .get() and explicitly cast (float(), str().upper(), bool()). This guarantees that missing or oddly formatted data won’t crash our boolean comparisons.
  • Fallback Safety: The else keyword handles T005, safely routing it to “Minimal Risk” because none of the previous thresholds were met.

13. Implemented Output

Plaintext
Txn T001 | Amount: $12,500.00 | Risk: High Risk
Txn T002 | Amount: $ 6,000.00 | Risk: Medium Risk
Txn T003 | Amount: $ 1,500.00 | Risk: Medium Risk
Txn T004 | Amount: $   800.00 | Risk: Low Risk
Txn T005 | Amount: $    50.00 | Risk: Minimal Risk

Coding5s System — Learn Programming by Writing Code

Scroll to Top