用python写一个学生信息管理系统 学生信息包含 姓名 性别 出生年月 班级 手机号码 宿舍号 基本功能包括添加删除修改查询退出使用自定义函数完成程序模块化
时间: 2024-06-06 20:06:36 浏览: 79
基于python实现学生信息管理系统
5星 · 资源好评率100%
# 学生信息管理系统
# 定义学生信息类
class Student:
def __init__(self, name, gender, birthday, grade, phone_number, dormitory):
self.name = name
self.gender = gender
self.birthday = birthday
self.grade = grade
self.phone_number = phone_number
self.dormitory = dormitory
# 定义学生信息管理类
class StudentManager:
def __init__(self):
self.students = []
# 添加学生信息
def add_student(self, name, gender, birthday, grade, phone_number, dormitory):
student = Student(name, gender, birthday, grade, phone_number, dormitory)
self.students.append(student)
print("添加成功!")
# 删除学生信息
def delete_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
print("删除成功!")
return
print("没有找到该学生!")
# 修改学生信息
def modify_student(self, name, gender=None, birthday=None, grade=None, phone_number=None, dormitory=None):
for student in self.students:
if student.name == name:
if gender is not None:
student.gender = gender
if birthday is not None:
student.birthday = birthday
if grade is not None:
student.grade = grade
if phone_number is not None:
student.phone_number = phone_number
if dormitory is not None:
student.dormitory = dormitory
print("修改成功!")
return
print("没有找到该学生!")
# 查询学生信息
def query_student(self, name):
for student in self.students:
if student.name == name:
print("姓名:%s 性别:%s 出生年月:%s 班级:%s 手机号码:%s 宿舍号:%s" % (student.name, student.gender, student.birthday, student.grade, student.phone_number, student.dormitory))
return
print("没有找到该学生!")
# 显示所有学生信息
def show_all_students(self):
for student in self.students:
print("姓名:%s 性别:%s 出生年月:%s 班级:%s 手机号码:%s 宿舍号:%s" % (student.name, student.gender, student.birthday, student.grade, student.phone_number, student.dormitory))
# 定义学生信息管理系统类
class StudentInfoSystem:
def __init__(self):
self.manager = StudentManager()
# 添加学生信息
def add_student(self):
name = input("请输入姓名:")
gender = input("请输入性别:")
birthday = input("请输入出生年月:")
grade = input("请输入班级:")
phone_number = input("请输入手机号码:")
dormitory = input("请输入宿舍号:")
self.manager.add_student(name, gender, birthday, grade, phone_number, dormitory)
# 删除学生信息
def delete_student(self):
name = input("请输入姓名:")
self.manager.delete_student(name)
# 修改学生信息
def modify_student(self):
name = input("请输入姓名:")
gender = input("请输入性别(不修改请按回车):")
birthday = input("请输入出生年月(不修改请按回车):")
grade = input("请输入班级(不修改请按回车):")
phone_number = input("请输入手机号码(不修改请按回车):")
dormitory = input("请输入宿舍号(不修改请按回车):")
self.manager.modify_student(name, gender, birthday, grade, phone_number, dormitory)
# 查询学生信息
def query_student(self):
name = input("请输入姓名:")
self.manager.query_student(name)
# 显示所有学生信息
def show_all_students(self):
self.manager.show_all_students()
# 主菜单
def main_menu(self):
print("欢迎使用学生信息管理系统!")
while True:
print("请选择功能:")
print("1. 添加学生信息")
print("2. 删除学生信息")
print("3. 修改学生信息")
print("4. 查询学生信息")
print("5. 显示所有学生信息")
print("6. 退出")
choice = input()
if choice == "1":
self.add_student()
elif choice == "2":
self.delete_student()
elif choice == "3":
self.modify_student()
elif choice == "4":
self.query_student()
elif choice == "5":
self.show_all_students()
elif choice == "6":
print("谢谢使用!")
break
else:
print("无效选项,请重新选择!")
# 启动学生信息管理系统
system = StudentInfoSystem()
system.main_menu()
阅读全文