请用python写一个200行的学生管理系统
时间: 2024-01-23 15:04:33 浏览: 68
好的,我可以提供一个简单的200行Python学生管理系统示例代码。但需要注意的是,这只是一个示例,实际应用中可能需要更多的代码和优化。
```python
import csv
# 定义学生类
class Student:
def __init__(self, name, age, gender, score):
self.name = name
self.age = age
self.gender = gender
self.score = score
# 学生管理系统类
class StudentManager:
def __init__(self):
self.students = []
self.load_data()
# 加载数据
def load_data(self):
try:
with open('students.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
name, age, gender, score = row
student = Student(name, int(age), gender, float(score))
self.students.append(student)
except FileNotFoundError:
pass
# 保存数据
def save_data(self):
with open('students.csv', 'w') as f:
writer = csv.writer(f)
for student in self.students:
writer.writerow([student.name, student.age, student.gender, student.score])
# 添加学生
def add_student(self, name, age, gender, score):
student = Student(name, age, gender, score)
self.students.append(student)
self.save_data()
# 删除学生
def delete_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
self.save_data()
return True
return False
# 修改学生信息
def modify_student(self, name, age, gender, score):
for student in self.students:
if student.name == name:
student.age = age
student.gender = gender
student.score = score
self.save_data()
return True
return False
# 查找学生
def find_student(self, name):
for student in self.students:
if student.name == name:
return student
return None
# 显示所有学生信息
def show_students(self):
for student in self.students:
print(f'姓名:{student.name},年龄:{student.age},性别:{student.gender},成绩:{student.score}')
# 主函数
def main():
manager = StudentManager()
while True:
print('===================================')
print('欢迎使用学生管理系统:')
print('1. 添加学生')
print('2. 删除学生')
print('3. 修改学生信息')
print('4. 查找学生')
print('5. 显示所有学生信息')
print('0. 退出系统')
print('===================================')
choice = input('请输入您的选择:')
if choice == '1':
name = input('请输入学生姓名:')
age = int(input('请输入学生年龄:'))
gender = input('请输入学生性别:')
score = float(input('请输入学生成绩:'))
manager.add_student(name, age, gender, score)
elif choice == '2':
name = input('请输入学生姓名:')
if manager.delete_student(name):
print('删除成功!')
else:
print('没有找到该学生!')
elif choice == '3':
name = input('请输入学生姓名:')
age = int(input('请输入学生年龄:'))
gender = input('请输入学生性别:')
score = float(input('请输入学生成绩:'))
if manager.modify_student(name, age, gender, score):
print('修改成功!')
else:
print('没有找到该学生!')
elif choice == '4':
name = input('请输入学生姓名:')
student = manager.find_student(name)
if student:
print(f'姓名:{student.name},年龄:{student.age},性别:{student.gender},成绩:{student.score}')
else:
print('没有找到该学生!')
elif choice == '5':
manager.show_students()
elif choice == '0':
print('感谢使用学生管理系统,再见!')
break
else:
print('输入有误,请重新输入!')
if __name__ == '__main__':
main()
```
以上代码实现了学生管理系统的基本功能,包括添加学生、删除学生、修改学生信息、查找学生和显示所有学生信息。数据以CSV格式存储在文件中,程序启动时会自动加载数据,退出程序时会自动保存数据。
阅读全文