Bird
0
0

This code tries to update an agent's knowledge but has a bug:

medium📝 Debug Q14 of 15
Agentic AI - Future of AI Agents
This code tries to update an agent's knowledge but has a bug:
actions = ['jump', 'run']
results = [True, False]
knowledge = {'jump': 0.3, 'run': 0.7}

for i in range(len(actions)):
    if results[i]:
        knowledge[actions[i]] += 0.1
    else:
        knowledge[actions[i]] =- 0.1

print(knowledge)
What is the bug and how to fix it?
AThe operator '= -' should be '-=' to subtract; fix: change to '-='.
BThe list lengths mismatch; fix by adding more results.
CThe dictionary keys are missing; fix by adding keys.
DThe print statement is incorrect; fix by using print(knowledge.values()).
Step-by-Step Solution
Solution:
  1. Step 1: Identify the incorrect operator

    The code uses '= - 0.1' which assigns negative 0.1 instead of subtracting.
  2. Step 2: Correct the operator to '-='

    Changing '= -' to '-=' correctly subtracts 0.1 from the current value.
  3. Final Answer:

    The operator '= -' should be '-=' to subtract; fix: change to '-='. -> Option A
  4. Quick Check:

    Use '-=' to subtract, not '= -' = C [OK]
Quick Trick: Use '-=' to subtract, not '= -' [OK]
Common Mistakes:
  • Confusing '= -' with '-=' operator
  • Ignoring operator syntax errors
  • Thinking print statement causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes