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.
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.
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):
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_accountis 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)
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
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
11. 🌳 Updated Code (V1.1 – FULLY IMPLEMENTED)
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/elsechain evaluates from top to bottom. T001 ($12,500) triggers theifblock immediately and skips the rest of the branch. - Compound Conditions: In the first
elif, we combined rules usingorandand. This allows T002 to match viaamount > 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
elsekeyword handles T005, safely routing it to “Minimal Risk” because none of the previous thresholds were met.
13. Implemented Output
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
