用字典统计Tom Marry和q的语文,数学,英语成绩,并计算Tom和Marry的语文,数学,英语总分
时间: 2023-05-27 07:07:57 浏览: 43
scores = {
"Tom": {
"语文": 90,
"数学": 80,
"英语": 85
},
"Marry": {
"语文": 95,
"数学": 90,
"英语": 92
},
"q": {
"语文": 80,
"数学": 70,
"英语": 85
}
}
# 计算总分
tom_total = sum(scores["Tom"].values())
marry_total = sum(scores["Marry"].values())
print("Tom总分为:", tom_total)
print("Marry总分为:", marry_total)
相关问题
用字典统计Tom Marry和q的语文,数学,英语成绩,并计算Tom和Marry的语文,数学,英语总分,并显示总人数,统计Tom Marry和q的语文,数学,英语的平均分
scores = {
'Tom': {'语文': 90, '数学': 80, '英语': 85},
'Marry': {'语文': 95, '数学': 75, '英语': 90},
'q': {'语文': 85, '数学': 90, '英语': 80}
}
# 统计Tom和Marry的语文,数学,英语总分
Tom_total = sum(scores['Tom'].values())
Marry_total = sum(scores['Marry'].values())
# 显示总人数
total_students = len(scores)
# 统计Tom Marry和q的语文,数学,英语的平均分
Chinese_avg = (scores['Tom']['语文'] + scores['Marry']['语文'] + scores['q']['语文']) / total_students
Math_avg = (scores['Tom']['数学'] + scores['Marry']['数学'] + scores['q']['数学']) / total_students
English_avg = (scores['Tom']['英语'] + scores['Marry']['英语'] + scores['q']['英语']) / total_students
print('Tom的总分为:', Tom_total)
print('Marry的总分为:', Marry_total)
print('总人数为:', total_students)
print('语文的平均分为:', Chinese_avg)
print('数学的平均分为:', Math_avg)
print('英语的平均分为:', English_avg)
怎么用字典统计三个学生的语文,数学,英语成绩,并且计算每个同学的总分和每个科目的平均分
以下是一个示例代码,假设三个学生的姓名分别为A、B、C:
```
# 定义字典存储成绩
scores = {
'A': {'语文': 80, '数学': 90, '英语': 70},
'B': {'语文': 70, '数学': 85, '英语': 90},
'C': {'语文': 90, '数学': 80, '英语': 75}
}
# 统计每个同学的总分和每个科目的总分
total_scores = {'A': 0, 'B': 0, 'C': 0}
subject_scores = {'语文': 0, '数学': 0, '英语': 0}
for name, score_dict in scores.items():
for subject, score in score_dict.items():
total_scores[name] += score
subject_scores[subject] += score
# 计算每个同学的平均分和每个科目的平均分
num_students = len(scores)
num_subjects = len(scores['A'])
average_scores = {'A': 0, 'B': 0, 'C': 0}
average_subjects = {'语文': 0, '数学': 0, '英语': 0}
for name, total_score in total_scores.items():
average_scores[name] = total_score / num_subjects
for subject, subject_score in subject_scores.items():
average_subjects[subject] = subject_score / num_students
# 输出结果
print('每个同学的总分:', total_scores)
print('每个同学的平均分:', average_scores)
print('每个科目的总分:', subject_scores)
print('每个科目的平均分:', average_subjects)
```
输出结果:
```
每个同学的总分: {'A': 240, 'B': 245, 'C': 245}
每个同学的平均分: {'A': 80.0, 'B': 81.66666666666667, 'C': 81.66666666666667}
每个科目的总分: {'语文': 240, '数学': 255, '英语': 235}
每个科目的平均分: {'语文': 80.0, '数学': 85.0, '英语': 78.33333333333333}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)