写一个Python程序: 输入三门课程的分数(整数),计算平均分后输出,要求保留两位小数。 点击复制后,将打开C知道体验页
时间: 2023-04-03 16:03:14 浏览: 442
你好!根据你的要求,我可以回答你的问题。以下是我的回答:
可以使用以下Python代码来计算三门课程的平均分并保留两位小数:
score1 = int(input("请输入第一门课程的分数:"))
score2 = int(input("请输入第二门课程的分数:"))
score3 = int(input("请输入第三门课程的分数:"))
average = (score1 + score2 + score3) / 3
print("三门课程的平均分为:%.2f" % average)
希望我的回答能够帮助到你,如果你还有其他问题,欢迎继续提问。
相关问题
在d:\s5.txt文件中,每行保存了学生的学号、三门课程的成绩。其中,学号包含10位数字,三门课程的成绩,均为整数,取值范围在[0,100]之内。 要求:计算三门课程的平均分数(保留一位小数),计算每位同学的总分,并保存到d:\t5.txt中
对于您的问题,我可以提供如下Python代码来解决:
```python
import numpy as np
# 读取文件中的数据
data = np.loadtxt("d:\s5.txt", dtype=np.int64)
# 计算每个学生的总分
total_score = np.sum(data[:, 1:], axis=1)
# 计算三门课的平均分
average_score = np.mean(data[:, 1:], axis=0)
# 保存每个学生的总分到文件
np.savetxt("d:\t5.txt", total_score, fmt='%d')
# 打印出三门课的平均分
print("三门课的平均分为:{:.1f},{:.1f},{:.1f}".format(*average_score))
```
希望这可以帮到您!
有n个学生,每个学生的数据包括学号,姓名, 三门课成绩,平均成绩。编写一个python程序,完成以 下功能: (1)输入一个正整数n下面n行输入n个学生的学号,姓名,和三门 课成绩; (2)计算每个学生的平均成绩并输出所有学生根据平均分从高到低排序后 的数据(包括该学生的学号,姓名,平均分(保留两位小数))
可以使用字典来保存每个学生的数据,然后计算平均分并排序输出。以下是实现代码:
```python
n = int(input("请输入学生人数:"))
students = []
for i in range(n):
print(f"请输入第{i+1}个学生的信息:")
student = {}
student['学号'] = input("学号:")
student['姓名'] = input("姓名:")
student['成绩1'] = float(input("成绩1:"))
student['成绩2'] = float(input("成绩2:"))
student['成绩3'] = float(input("成绩3:"))
student['平均分'] = round((student['成绩1'] + student['成绩2'] + student['成绩3']) / 3, 2)
students.append(student)
students = sorted(students, key=lambda x: x['平均分'], reverse=True)
print("按平均分从高到低排序后的学生信息:")
for student in students:
print(f"学号:{student['学号']},姓名:{student['姓名']},平均分:{student['平均分']}")
```
运行程序后,可以按照提示输入学生信息,最后输出按平均分从高到低排序后的学生信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)