Bird
0
0

Consider this Python code snippet for sending a message between agents:

medium📝 Debug Q14 of 15
Agentic AI - Future of AI Agents
Consider this Python code snippet for sending a message between agents:
def send_message(msg):
    print(f"Sending from {msg['sender']} to {msg['receiver']}")

message = {"sender": "AgentX", "content": "Hello"}
send_message(message)

What error will occur when running this code?
ATypeError because message is not a string
BKeyError because 'receiver' key is missing in message
CSyntaxError due to incorrect print statement
DNo error, prints message successfully
Step-by-Step Solution
Solution:
  1. Step 1: Check message dictionary keys

    The message dictionary has 'sender' and 'content' but lacks 'receiver'.
  2. Step 2: Analyze print statement access

    The print tries to access msg['receiver'], which is missing, causing KeyError.
  3. Final Answer:

    KeyError because 'receiver' key is missing in message -> Option B
  4. Quick Check:

    Missing key access = KeyError [OK]
Quick Trick: Check all keys exist before accessing [OK]
Common Mistakes:
  • Assuming missing keys default to None
  • Thinking print syntax is wrong
  • Confusing KeyError with TypeError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes