Bird
0
0

Why does this budget control code fail?

medium📝 Debug Q7 of 15
Agentic AI - Agent Safety and Guardrails
Why does this budget control code fail?
budget = 50
costs = [20, 40, 10]
spent = 0
for cost in costs:
    if spent + cost < budget:
        spent += cost
print(spent)
AIt adds costs even if budget is exceeded
BIt excludes costs equal to budget, missing valid spends
CVariable 'spent' is not initialized
DLoop syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check condition for adding costs

    Condition uses < budget, so equal costs are excluded.
  2. Step 2: Identify impact on spending

    Cost 40 + spent 20 = 60 > 50, so 40 skipped; 10 added later.
  3. Final Answer:

    It excludes costs equal to budget, missing valid spends -> Option B
  4. Quick Check:

    Use <= to include equal budget spends [OK]
Quick Trick: Use <= to include costs equal to budget [OK]
Common Mistakes:
  • Using < excludes valid equal spends
  • Assuming loop or variables are wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes