python计算学生总成绩
时间: 2023-10-25 13:23:43 浏览: 115
假设你有一个包含学生姓名、语文成绩、数学成绩、英语成绩的字典列表,可以使用以下代码计算每个学生的总成绩:
```python
students = [
{"name": "Alice", "chinese": 90, "math": 85, "english": 95},
{"name": "Bob", "chinese": 80, "math": 90, "english": 85},
{"name": "Charlie", "chinese": 95, "math": 95, "english": 90},
]
for student in students:
total = student["chinese"] + student["math"] + student["english"]
print(f"{student['name']}'s total score is {total}")
```
输出结果为:
```
Alice's total score is 270
Bob's total score is 255
Charlie's total score is 280
```
你也可以将每个学生的总成绩保存在原始字典中,例如:
```python
for student in students:
student["total"] = student["chinese"] + student["math"] + student["english"]
print(f"{student['name']}'s total score is {student['total']}")
```
这样每个学生的字典会变成:
```python
{"name": "Alice", "chinese": 90, "math": 85, "english": 95, "total": 270}
```
阅读全文