有了之前c++做通讯录系统的基础,这个相对于就简单一点了
系统要求:学员数据存储在⽂件中
系统功能:添加学员、删除学员、修改学员信息、查询学员信息、显示所有学员信息,退出系统等功能。
直接写列程序吧
student.py
class Student():
def __init__(self,name,gender,tell):
self.name=name
self.gender=gender
self.tell=tell
def __str__(self):
return f'{self.name}\t{self.gender}\t{self.tell}'
studentmanager.py
from student import *
class StudentManager():
def __init__(self):
self.student_list=[]
def run(self):
self.load_student()
while True:
self.show_menu()
menu_num=int(input("请输入你需要的功能序号"))
if menu_num==1:
self.add_student()
elif menu_num==2:
self.del_student()
elif menu_num==3:
self.modify_student()
elif menu_num==4:
self.search_student()
elif menu_num==5:
self.show_student()
elif menu_num==6:
break
def show_menu(self):
print('请选择如下功能-----------------')
print('1:添加学员')
print('2:删除学员')
print('3:修改学员信息')
print('4:查询学员信息')
print('5:显示所有学员信息')
print('6:退出系统')
def add_student(self):
name=input('请输入你的姓名')
gender=input('请输入你的性别')
tell=input('请输入你的手机号')
student=Student(name,gender,tell)
self.student_list.append(student)
#print(self.student_list)
self.save_student()
print('添加成功')
self.show_student()
def del_student(self):
del_name=input('请输入要删除的学员姓名')
for i in self.student_list:
if(i.name==del_name):
self.student_list.remove(i)
print('已删除该学员')
break
else:
print("学员不存在")
self.show_student()
self.save_student()
def modify_student(self):
modify_name=input('请输入要修改的学员姓名')
for i in self.student_list:
if(i.name==modify_name):
i.name=input('请输入修改后的学员姓名')
i.gender = input('请输入修改后的学员性别')
i.tell = input('请输入修改后的学员手机号')
else:
print("未找到该学员,请重新选择")
self.save_student()
def search_student(self):
search_name=input("请输入要查询的学员姓名")
for i in self.student_list:
if(i.name==search_name):
print(i)
break
else:
print("无此学员")
def show_student(self):
print('姓名\t性别\t手机号')
for i in self.student_list:
print(i)
def save_student(self):
f=open('student.data','w')
new_list=[i.__dict__ for i in self.student_list]
#print(new_list)
f.write(str(new_list))
f.close()
def load_student(self):
try:
f=open('student.data','r')
except:
f=open("student.1data",'w')
else:
data=f.read()
new_list=eval(data)
self.student_list=[Student(i['name'],i['gender'],i['tell']) for i in new_list]
finally:
f.close()
main.py
from managerSystem import *
if __name__=='__main__':
student_manager=StudentManager()
student_manager.run()