# G4-05 课后练习 —— 复合嵌套上
> **GESP等级：四级 | 考级考点：GESP Python 四级**

**姓名：__________  日期：__________**

---

> 面向7-10岁少儿 | G4 编程高手

---

## 一、填空题（回顾概念）

**题目1：** 在 `students = [{"name": "小明", "score": 90}, {"name": "小红", "score": 85}]` 中，要访问小红的分数，写法是 `students[____][____]`。

**题目2：** 字典嵌套列表：`data = {"fruits": ["苹果", "香蕉", "橙子"]}` 中，要访问"香蕉"，写法是 `data[____][____]`。

---

## 二、代码补全（补写代码）

**题目3：** 有一个班级成绩数据（列表套字典），请计算每位同学的总分和平均分。补全代码。

```python
students = [
    {"name": "小明", "chinese": 90, "math": 85, "english": 88},
    {"name": "小红", "chinese": 95, "math": 92, "english": 90},
    {"name": "小刚", "chinese": 78, "math": 88, "english": 82}
]

for stu in students:
    total = stu[____] + stu[____] + stu[____]
    avg = total / ____
    print(f"{stu[____]}：总分{total}，平均{avg:.1f}")
```

**题目4：** 用双层列表推导式生成一个九九乘法表（只保留结果不为空的项）。补全代码。

```python
# 用双层for循环
results = []
for i in range(1, ____):
    for j in range(1, ____ + 1):
        results.append(f"{i}x{j}={i*j}")
print(results)

# 用双层列表推导式（一行搞定）
results2 = [f"{i}x{j}={i*j}" for i in range(1, 10) for j in range(1, ____)]
print(results2)
```

---

## 三、创作题（动手编程）

**题目5：** 编写"学生通讯录管理系统"（用列表套字典的嵌套结构）。
- 创建通讯录数据：
  ```python
  contacts = [
      {"name": "小明", "phone": "13800001111", "city": "北京"},
      {"name": "小红", "phone": "13800002222", "city": "上海"},
      {"name": "小刚", "phone": "13800003333", "city": "广州"}
  ]
  ```
- 实现以下功能：
  1. 打印所有人的姓名和电话
  2. 添加一个新联系人：小丽，电话13800004444，城市深圳
  3. 查找城市为"北京"的所有人
  4. 用列表推导式生成所有联系人的姓名列表

请写出完整代码。

<details>
<summary>参考答案</summary>

**题目1：** `students[1]["score"]` 或 `students[1]["score"]`

**题目2：** `data["fruits"][1]`

**题目3：**
```python
students = [
    {"name": "小明", "chinese": 90, "math": 85, "english": 88},
    {"name": "小红", "chinese": 95, "math": 92, "english": 90},
    {"name": "小刚", "chinese": 78, "math": 88, "english": 82}
]

for stu in students:
    total = stu["chinese"] + stu["math"] + stu["english"]
    avg = total / 3
    print(f"{stu['name']}：总分{total}，平均{avg:.1f}")
```

**题目4：**
```python
results = []
for i in range(1, 10):
    for j in range(1, i + 1):
        results.append(f"{i}x{j}={i*j}")
print(results)

results2 = [f"{i}x{j}={i*j}" for i in range(1, 10) for j in range(1, i + 1)]
print(results2)
```

**题目5：**
```python
contacts = [
    {"name": "小明", "phone": "13800001111", "city": "北京"},
    {"name": "小红", "phone": "13800002222", "city": "上海"},
    {"name": "小刚", "phone": "13800003333", "city": "广州"}
]

# 1. 打印所有人
print("===== 通讯录 =====")
for c in contacts:
    print(f"{c['name']}：{c['phone']}")

# 2. 添加新联系人
contacts.append({"name": "小丽", "phone": "13800004444", "city": "深圳"})
print(f"\n已添加：小丽")

# 3. 查找北京的人
beijing = [c for c in contacts if c["city"] == "北京"]
print(f"\n北京联系人：{[c['name'] for c in beijing]}")

# 4. 所有人名列表
names = [c["name"] for c in contacts]
print(f"\n所有联系人：{names}")
```
</details>


---

**自我评价：** □ 全都会了  □ 大部分会了  □ 还要再练练
