创建函数count_reader,统计给定类别编号读者的数量。然后调用函数,分别查看读者类别是1和2的读者数量。
时间: 2024-03-23 20:37:11 浏览: 13
好的,下面是一个示例 Python 代码,实现了所述功能:
```python
# 假设读者信息保存在一个列表中,每个读者有一个属性 category 表示其类别编号
readers = [
{'name': 'Alice', 'age': 23, 'category': 1},
{'name': 'Bob', 'age': 34, 'category': 1},
{'name': 'Charlie', 'age': 19, 'category': 2},
{'name': 'David', 'age': 41, 'category': 1},
{'name': 'Eve', 'age': 28, 'category': 2},
]
# 定义统计函数
def count_reader(category):
count = 0
for reader in readers:
if reader['category'] == category:
count += 1
return count
# 调用函数,查看类别为1和2的读者数量
print('类别1的读者数量:', count_reader(1))
print('类别2的读者数量:', count_reader(2))
```
输出结果如下:
```
类别1的读者数量: 3
类别2的读者数量: 2
```
注意,这只是一个示例代码,实际情况可能需要根据具体的数据结构和需求进行修改。