Home › Interview Questions › Why does the code raise a KeyError when accessing …

Why does the code raise a KeyError when accessing player['Name'] instead of player['name']?

🟢 Easy Debugging Junior level
2Times asked
Oct 2026Last seen
Oct 2026First seen

💡 Model Answer

Python dictionaries are case‑sensitive, so the key 'Name' is different from 'name'. In the provided data the dictionary uses the key 'name', but the code attempts to access 'Name', which does not exist, causing a KeyError. The fix is to use the correct key: player['name']. Consistency in key naming is essential; if you need to support both cases, you could normalize keys or use a case‑insensitive mapping. The corrected code appends the player dictionary with the proper key: groups['Under 30'].append({'name': player['name'], 'age': player['age'], 'reward': player['reward']}). This resolves the error and ensures the data is stored correctly.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500