简单学生成绩管理系统Python代码
时间: 2024-06-01 09:06:15 浏览: 20
好的,简单的学生成绩管理系统Python代码可以通过以下几个步骤实现:
1. 定义一个学生类,包含学生姓名、学号、年级等信息,并定义一个成绩列表,用于存储该学生的各科成绩。
2. 定义一个成绩管理类,包含添加学生、删除学生、修改学生信息、查询学生信息、计算学生平均成绩等功能。
3. 使用文件读写操作,将学生信息和成绩保存到文件中,以便下次使用时可以读取。
4. 实现一个用户界面,通过用户输入选择相应的功能,从而实现对学生成绩的管理。
以下是一个简单的示例代码,仅供参考:
```python
import pickle
class Student:
def __init__(self, name, id, grade):
self.name = name
self.id = id
self.grade = grade
self.scores = []
class ScoreManager:
def __init__(self):
self.students = []
self.load_data()
def add_student(self, student):
self.students.append(student)
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
break
def modify_student(self, id, name=None, grade=None):
for student in self.students:
if student.id == id:
if name:
student.name = name
if grade:
student.grade = grade
break
def query_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
def calc_average_score(self, id):
student = self.query_student(id)
if student:
scores = student.scores
return sum(scores) / len(scores)
return None
def save_data(self):
with open('data.pkl', 'wb') as f:
pickle.dump(self.students, f)
def load_data(self):
try:
with open('data.pkl', 'rb') as f:
self.students = pickle.load(f)
except FileNotFoundError:
pass
def main():
sm = ScoreManager()
while True:
print('请选择功能:')
print('1. 添加学生')
print('2. 删除学生')
print('3. 修改学生信息')
print('4. 查询学生信息')
print('5. 计算平均分')
print('6. 退出')
choice = input('请输入序号:')
if choice == '1':
name = input('请输入姓名:')
id = input('请输入学号:')
grade = input('请输入年级:')
student = Student(name, id, grade)
sm.add_student(student)
sm.save_data()
print('添加成功!')
elif choice == '2':
id = input('请输入要删除的学号:')
sm.remove_student(id)
sm.save_data()
print('删除成功!')
elif choice == '3':
id = input('请输入要修改的学号:')
name = input('请输入新姓名(回车表示不修改):')
grade = input('请输入新年级(回车表示不修改):')
sm.modify_student(id, name, grade)
sm.save_data()
print('修改成功!')
elif choice == '4':
id = input('请输入要查询的学号:')
student = sm.query_student(id)
if student:
print(f'姓名:{student.name} 学号:{student.id} 年级:{student.grade} 成绩:{student.scores}')
else:
print('查询失败!')
elif choice == '5':
id = input('请输入要计算平均分的学号:')
avg_score = sm.calc_average_score(id)
if avg_score:
print(f'平均分为:{avg_score:.2f}')
else:
print('计算失败!')
elif choice == '6':
print('再见!')
break
if __name__ == '__main__':
main()
```
相关推荐
![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)