编写一个简单的学生信息管理程序及菜单系统。 工定义学生类和管理员类。以学生类为基础,派生出本科生类和研究生类。学生包括学号、 姓名、性别、年龄、专业、班级、成绩等成员:研究生还包括导师等信息,本科生还包括学制 (4年或3年)等信息。
时间: 2023-12-28 11:03:47 浏览: 87
学生信息管理系统,实现学生信息包括:学号、姓名、性别、年龄、班级等信息。
5星 · 资源好评率100%
好的,我可以帮你编写一个简单的学生信息管理程序及菜单系统。首先需要定义学生类和管理员类,代码如下:
```python
class Student:
def __init__(self, id, name, gender, age, major, clazz, score):
self.id = id
self.name = name
self.gender = gender
self.age = age
self.major = major
self.clazz = clazz
self.score = score
class Undergraduate(Student):
def __init__(self, id, name, gender, age, major, clazz, score, year):
super().__init__(id, name, gender, age, major, clazz, score)
self.year = year
class Graduate(Student):
def __init__(self, id, name, gender, age, major, clazz, score, advisor):
super().__init__(id, name, gender, age, major, clazz, score)
self.advisor = advisor
class Admin:
def __init__(self, username, password):
self.username = username
self.password = password
```
接下来,我们可以定义一些方法来实现学生信息的增删改查功能,代码如下:
```python
class StudentManager:
def __init__(self):
self.students = []
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)
return True
return False
def update_student(self, id, name, gender, age, major, clazz, score, year=None, advisor=None):
for student in self.students:
if student.id == id:
student.name = name
student.gender = gender
student.age = age
student.major = major
student.clazz = clazz
student.score = score
if isinstance(student, Undergraduate):
student.year = year
elif isinstance(student, Graduate):
student.advisor = advisor
return True
return False
def get_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
def get_all_students(self):
return self.students
```
最后,我们可以编写一个菜单系统来实现用户与程序的交互,代码如下:
```python
def main():
student_manager = StudentManager()
admin = Admin('admin', '123456')
while True:
print('欢迎使用学生信息管理系统')
username = input('请输入管理员用户名:')
password = input('请输入管理员密码:')
if username == admin.username and password == admin.password:
print('登录成功!')
break
else:
print('用户名或密码错误,请重新输入!')
while True:
print('请选择操作:')
print('1. 添加学生')
print('2. 删除学生')
print('3. 修改学生信息')
print('4. 查询学生信息')
print('5. 显示所有学生信息')
print('0. 退出系统')
choice = input('请输入操作编号:')
if choice == '1':
id = input('请输入学生学号:')
name = input('请输入学生姓名:')
gender = input('请输入学生性别:')
age = input('请输入学生年龄:')
major = input('请输入学生专业:')
clazz = input('请输入学生班级:')
score = input('请输入学生成绩:')
category = input('请输入学生类别(1:本科生,2:研究生):')
if category == '1':
year = input('请输入学制(4年或3年):')
student = Undergraduate(id, name, gender, age, major, clazz, score, year)
elif category == '2':
advisor = input('请输入导师姓名:')
student = Graduate(id, name, gender, age, major, clazz, score, advisor)
student_manager.add_student(student)
print('添加成功!')
elif choice == '2':
id = input('请输入学生学号:')
if student_manager.remove_student(id):
print('删除成功!')
else:
print('学生不存在!')
elif choice == '3':
id = input('请输入学生学号:')
name = input('请输入学生姓名:')
gender = input('请输入学生性别:')
age = input('请输入学生年龄:')
major = input('请输入学生专业:')
clazz = input('请输入学生班级:')
score = input('请输入学生成绩:')
student = student_manager.get_student(id)
if isinstance(student, Undergraduate):
year = input('请输入学制(4年或3年):')
student_manager.update_student(id, name, gender, age, major, clazz, score, year=year)
elif isinstance(student, Graduate):
advisor = input('请输入导师姓名:')
student_manager.update_student(id, name, gender, age, major, clazz, score, advisor=advisor)
else:
student_manager.update_student(id, name, gender, age, major, clazz, score)
print('修改成功!')
elif choice == '4':
id = input('请输入学生学号:')
student = student_manager.get_student(id)
if student is not None:
print('学号:', student.id)
print('姓名:', student.name)
print('性别:', student.gender)
print('年龄:', student.age)
print('专业:', student.major)
print('班级:', student.clazz)
print('成绩:', student.score)
if isinstance(student, Undergraduate):
print('学制:', student.year)
elif isinstance(student, Graduate):
print('导师:', student.advisor)
else:
print('学生不存在!')
elif choice == '5':
students = student_manager.get_all_students()
if len(students) > 0:
for student in students:
print('学号:', student.id)
print('姓名:', student.name)
print('性别:', student.gender)
print('年龄:', student.age)
print('专业:', student.major)
print('班级:', student.clazz)
print('成绩:', student.score)
if isinstance(student, Undergraduate):
print('学制:', student.year)
elif isinstance(student, Graduate):
print('导师:', student.advisor)
else:
print('没有学生信息!')
elif choice == '0':
print('谢谢使用!')
break
else:
print('输入有误,请重新输入!')
if __name__ == '__main__':
main()
```
这样,我们就完成了一个简单的学生信息管理程序及菜单系统。用户可以通过不同的操作来实现对学生信息的增删改查功能。
阅读全文