python写一个学生管理系统具有学生的学号,姓名,性别,年龄,科目,成绩,可以增加学生信息,修改学生信息,删除学生信息,查询学生信息,查询成绩的平均值,查询成绩排序,学生成绩的修改
时间: 2024-05-17 07:19:50 浏览: 17
好的,这个问题涉及到了比较多的知识点,我们可以分步骤来实现。
首先,我们需要定义一个学生类,包含学号、姓名、性别、年龄、科目、成绩等属性。可以使用字典来表示一个学生的信息。
```python
class Student:
def __init__(self, id, name, sex, age, subject, score):
self.id = id
self.name = name
self.sex = sex
self.age = age
self.subject = subject
self.score = score
def __str__(self):
return f'{self.id}\t{self.name}\t{self.sex}\t{self.age}\t{self.subject}\t{self.score}'
```
接下来,我们需要实现一个学生管理系统类,包含增加学生、修改学生、删除学生、查询学生、查询成绩平均值、查询成绩排序、学生成绩修改等功能。
```python
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def modify_student(self, id, name=None, sex=None, age=None, subject=None, score=None):
for student in self.students:
if student.id == id:
if name:
student.name = name
if sex:
student.sex = sex
if age:
student.age = age
if subject:
student.subject = subject
if score:
student.score = score
return True
return False
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
return True
return False
def query_student(self, id=None, name=None, sex=None, age=None, subject=None, score=None):
result = []
for student in self.students:
if (not id or student.id == id) and \
(not name or student.name == name) and \
(not sex or student.sex == sex) and \
(not age or student.age == age) and \
(not subject or student.subject == subject) and \
(not score or student.score == score):
result.append(student)
return result
def query_score_average(self, subject):
scores = []
for student in self.students:
if student.subject == subject:
scores.append(student.score)
if len(scores) > 0:
return sum(scores) / len(scores)
else:
return 0
def query_score_sort(self, subject):
scores = []
for student in self.students:
if student.subject == subject:
scores.append(student.score)
return sorted(scores, reverse=True)
def modify_score(self, id, subject, score):
for student in self.students:
if student.id == id and student.subject == subject:
student.score = score
return True
return False
```
现在我们可以使用学生管理系统来进行增加、修改、删除、查询学生等操作了。
```python
sms = StudentManagementSystem()
# 添加学生
s1 = Student('1001', '张三', '男', 20, '语文', 90)
s2 = Student('1002', '李四', '女', 21, '数学', 95)
sms.add_student(s1)
sms.add_student(s2)
# 修改学生信息
sms.modify_student('1001', name='王五', age=22)
# 删除学生
sms.remove_student('1002')
# 查询学生
print(sms.query_student(id='1001'))
# 查询成绩平均值
print(sms.query_score_average('语文'))
# 查询成绩排序
print(sms.query_score_sort('语文'))
# 修改成绩
sms.modify_score('1001', '语文', 95)
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)
![](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)