Bird
0
0

Why does this code raise a KeyError?

medium📝 Debug Q7 of 15
NLP - Word Embeddings
Why does this code raise a KeyError?
embedding = {'sun': [0.5, 0.6]}
print(embedding['moon'])
ABecause 'moon' key is not in the embedding dictionary
BBecause the vector values are floats
CBecause the dictionary is empty
DBecause print() cannot print lists
Step-by-Step Solution
Solution:
  1. Step 1: Check dictionary keys

    embedding contains only 'sun' key, not 'moon'.
  2. Step 2: Understand KeyError cause

    Accessing a missing key with [] raises KeyError.
  3. Final Answer:

    Because 'moon' key is not in the embedding dictionary -> Option A
  4. Quick Check:

    Missing key access = KeyError [OK]
Quick Trick: Access missing dict key with [] causes KeyError [OK]
Common Mistakes:
MISTAKES
  • Thinking float values cause error
  • Assuming dictionary is empty
  • Believing print() cannot print lists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes