存储学生Zhang成绩的字典结构如下: stu={"name":"Zhang","english":80,"python":90,"math":100} 1)请使用上述结构输入学生的name及english、python和math三门课的成绩并存储到字典stu中(不考虑空数据的情况); 2)计算该同学的平均成绩,同时在字典中添加关键字"avg"用来表示平均成绩; 3)由高到低排序该学生的各科成绩; 4)输出该学生的姓名,各科成绩(保留两位小数)和平均成绩(保留两位小数)。
时间: 2023-07-19 12:05:45 浏览: 71
1. 输入学生的name及english、python和math三门课的成绩并存储到字典stu中:
```python
stu = {"name": "Zhang"}
stu["english"] = 80
stu["python"] = 90
stu["math"] = 100
```
2. 计算该同学的平均成绩,同时在字典中添加关键字"avg"用来表示平均成绩:
```python
avg_score = (stu["english"] + stu["python"] + stu["math"]) / 3
stu["avg"] = avg_score
```
3. 由高到低排序该学生的各科成绩:
```python
sorted_scores = sorted(stu.items(), key=lambda x: x[1], reverse=True)
```
4. 输出该学生的姓名,各科成绩(保留两位小数)和平均成绩(保留两位小数):
```python
print(f"姓名:{stu['name']}")
for subject, score in sorted_scores[1:]:
print(f"{subject}:{score:.2f}")
print(f"平均成绩:{avg_score:.2f}")
```
完整代码如下:
```python
stu = {"name": "Zhang"}
stu["english"] = 80
stu["python"] = 90
stu["math"] = 100
avg_score = (stu["english"] + stu["python"] + stu["math"]) / 3
stu["avg"] = avg_score
sorted_scores = sorted(stu.items(), key=lambda x: x[1], reverse=True)
print(f"姓名:{stu['name']}")
for subject, score in sorted_scores[1:]:
print(f"{subject}:{score:.2f}")
print(f"平均成绩:{avg_score:.2f}")
```
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)