用python设计一个学生类(Student),包含学生的学号(sid)、姓名和Python课程的成绩(scor e)。计算3名学生的平均成 绩(average)并输出。
时间: 2024-03-10 21:48:17 浏览: 139
可以按照以下代码来实现:
```python
class Student:
def __init__(self, sid, name, score):
self.sid = sid
self.name = name
self.score = score
s1 = Student("001", "Tom", 90)
s2 = Student("002", "Jerry", 80)
s3 = Student("003", "Alice", 70)
total_score = s1.score + s2.score + s3.score
average_score = total_score / 3
print("Average score: {:.2f}".format(average_score))
```
这里首先定义了一个 `Student` 类,包含 `sid`、`name` 和 `score` 三个属性。然后创建了三个 `Student` 对象分别表示三名学生,并计算了他们的总分和平均分。最后输出平均分,使用了 `format()` 方法保留了两位小数。
相关问题
使用MySQL查询平均分前30%的学生的各科成绩及平均分字段名为StuName 学生姓名,CBT_scor 计算机应用基础成绩,PS_score PS成绩,H5_score H5成绩 FROM t_student JOIN t_score on t_score.StuId=t_student.StuId
你可以使用以下SQL语句查询平均分前30%的学生的各科成绩及平均分:
```
SELECT StuName, CBT_score, PS_score, H5_score
FROM (
SELECT *, (@rank:=@rank+1) AS rank
FROM (
SELECT t_student.StuName, AVG(t_score.CBT_score) AS CBT_score, AVG(t_score.PS_score) AS PS_score, AVG(t_score.H5_score) AS H5_score, AVG(t_score.total_score) AS total_score
FROM t_student
JOIN t_score ON t_student.StuId = t_score.StuId
GROUP BY t_student.StuName
ORDER BY total_score DESC
) AS ranked, (SELECT @rank:=0) AS r
) AS top30
WHERE rank <= (SELECT COUNT(*) * 0.3 FROM t_student)
```
这个查询语句首先计算每个学生的平均分,并按照平均分从高到低进行排名。然后再从排名中选择前30%的学生,并返回他们的姓名、计算机应用基础成绩、PS成绩、H5成绩。注意,这个查询语句中的t_student和t_score分别是学生信息表和成绩表的表名,你需要根据具体情况进行修改。
if a==2: id2 = input("请输入要修改的学生学号:") modfile_file1 = open("C:\\Users\\ASUS\\Desktop\\student.txt","r",encoding="UTF-8") # 以可读方式打开文件,读取到line_info中,每一行就是一个列表的元素 line_info = modfile_file1.readlines() modfile_file2 = open("C:\\Users\\ASUS\\Desktop\\student.txt","w",encoding="UTF-8") # 用以写入修改后的数据 modfile_file2.close() for line in line_info: # 遍历列表 if id2 in line: # 如果修改的学生信息存在,就重新写入学生信息 print("已找到,请修改信息!") name = input("请输入姓名:") score_chi = input("请输入语文成绩:") score_math = input("请输入数学成绩:") score_eng = input("请输入英语成绩:") score_py = input("请输入python成绩:") sum_score = int(scor_chi)+int(score_eng) + int(score_py) + int(score_math) #stu_info = id2 + '\t' + name + '\t' + score_eng + '\t' + score_py + '\t' + \mod_score_math + '\t' + str(mod_sum_score) + '\n' modfile_file2.write(sum_score) print("修改成功!!!") continue modfile_file2.write(line) 怎么修改这段代码
这段代码有一些问题,需要进行修改。具体来说:
1. 在打开文件 `modfile_file2` 后,应该先写入标题行,否则修改后的文件将缺少标题行。
2. 在修改学生信息时,需要将输入的成绩转换为整型后再进行加和操作。
3. 在写入修改后的学生信息时,需要将学生信息拼接成一个字符串后再写入文件,否则会报错。
下面是修改后的代码:
```
if a == 2:
id2 = input("请输入要修改的学生学号:")
modfile_file1 = open("C:\\Users\\ASUS\\Desktop\\student.txt", "r", encoding="UTF-8")
line_info = modfile_file1.readlines()
modfile_file1.close()
modfile_file2 = open("C:\\Users\\ASUS\\Desktop\\student.txt", "w", encoding="UTF-8")
modfile_file2.write("学号\t姓名\t语文\t数学\t英语\tPython\t总分\n") # 写入标题行
for line in line_info:
if id2 in line:
print("已找到,请修改信息!")
name = input("请输入姓名:")
score_chi = input("请输入语文成绩:")
score_math = input("请输入数学成绩:")
score_eng = input("请输入英语成绩:")
score_py = input("请输入python成绩:")
sum_score = int(score_chi) + int(score_eng) + int(score_py) + int(score_math)
stu_info = id2 + '\t' + name + '\t' + score_chi + '\t' + score_math + '\t' + score_eng + '\t' + score_py + '\t' + str(sum_score) + '\n'
modfile_file2.write(stu_info)
print("修改成功!!!")
else:
modfile_file2.write(line)
modfile_file2.close()
```
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.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)