Bird
0
0

Identify the error in this code that counts items in training data:

medium📝 Analysis Q6 of 15
AI for Everyone - How AI Models Actually Work
Identify the error in this code that counts items in training data:
training_data = ['x', 'y', 'x']
counts = {}
for item in training_data:
    counts[item] += 1
print(counts)
AThe list 'training_data' is empty
BThe loop should use 'counts.get(item)' instead of 'counts[item]'
CThe print statement is missing parentheses
DThe dictionary keys are not initialized before incrementing
Step-by-Step Solution
Solution:
  1. Step 1: Understand dictionary increment

    Incrementing a key requires it to exist first.
  2. Step 2: Check code behavior

    Since keys are not initialized, 'counts[item] += 1' raises a KeyError.
  3. Final Answer:

    The dictionary keys are not initialized before incrementing -> Option D
  4. Quick Check:

    Initialize keys before incrementing [OK]
Quick Trick: Initialize dictionary keys before incrementing [OK]
Common Mistakes:
  • Assuming keys auto-initialize
  • Confusing syntax errors with logic errors
  • Ignoring error messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More AI for Everyone Quizzes