Bird
0
0

What is wrong with this output filtering snippet?

medium📝 Debug Q7 of 15
Agentic AI - Agent Safety and Guardrails
What is wrong with this output filtering snippet?
banned_words = ["spam", "scam"]
output = "This is a scam message"
for word in banned_words:
    if word == output:
        print("Blocked")
    else:
        print("Allowed")
APrint statements should be outside the loop
BThe for loop syntax is invalid
Cbanned_words should be a set, not a list
DIt compares whole output to banned word instead of checking containment
Step-by-Step Solution
Solution:
  1. Step 1: Analyze comparison logic

    The code compares if banned word equals the entire output string, which is false unless exact match.
  2. Step 2: Correct containment check

    Should check if banned word is contained in output using 'if word in output'.
  3. Final Answer:

    It compares whole output to banned word instead of checking containment -> Option D
  4. Quick Check:

    Use 'in' to check substring, not equality [OK]
Quick Trick: Use 'in' to check if banned word is inside output [OK]
Common Mistakes:
  • Using equality instead of containment
  • Misplacing print statements
  • Thinking list type affects containment check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes