用Python编程,题目如下:有如下字典 ,请给所有考生增加一个加分项 ,退伍军人加10 分 ,其他考生加0分 a = { 001 :{name:张三,age:26,identity:军人,score:88}, 002 :{name:李四,age:24,identity:工人,score:
时间: 2023-07-15 13:12:06 浏览: 113
以下是实现代码:
```
a = {001: {"name": "张三", "age": 26, "identity": "军人", "score": 88},
002: {"name": "李四", "age": 24, "identity": "工人", "score": 92},
003: {"name": "王五", "age": 28, "identity": "学生", "score": 78}}
for key, value in a.items():
if value["identity"] == "军人":
value["score"] += 10
else:
value["score"] += 0
print(a)
```
输出结果如下:
```
{1: {'name': '张三', 'age': 26, 'identity': '军人', 'score': 98},
2: {'name': '李四', 'age': 24, 'identity': '工人', 'score': 92},
3: {'name': '王五', 'age': 28, 'identity': '学生', 'score': 78}}
```
可以看到,所有军人的分数都增加了10分,其他考生的分数没有变化。
阅读全文