# 键错误：'missing_key'

- **ID:** `python/keyerror-key-not-found-in-dict`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

测试尝试访问字典中不存在的键。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |

## 解决方案

1. **Use dict.get() with a default value** (95% 成功率)
   ```
   value = my_dict.get('missing_key', 'default')
   ```
2. **Check key existence before access** (90% 成功率)
   ```
   if 'missing_key' in my_dict: value = my_dict['missing_key']
   ```

## 无效尝试

- **Using dict.get() without default** — Returns None, which may cause subsequent errors if None is not handled. (50% 失败率)
- **Catching KeyError silently** — Silent catch hides the missing key and may lead to incorrect test results. (70% 失败率)
