1.主界面模块
"""__author__=wuliang"""
from time import sleep
import logout
import login
import student
import file_operation
'''
主界面模块,也是被程序的入口
'''
def start_inter():
'''
系统初始化的主界面
:return:
'''
print('='*30)
print('+',end='')
print(' '*7,end='')
print('学生信息管理系统',end='')
print(' '*5,end='')
print('+')
print('+', end='')
print(' '*10,end='')
print('1.登录',end='')
print(' '*10,end='')
print('+')
print('+', end='')
print(' ' * 10, end='')
print('2.注册', end='')
print(' ' * 10, end='')
print('+')
print('+', end='')
print(' ' * 10, end='')
print('3.退出', end='')
print(' ' * 10, end='')
print('+')
print("="*30)
operation=input('请输入你的选择:')
fun_operation(operation)
def main_operation(username):
'''
系统操作主界面
:return:
'''
operation=0 #设定operation初始值0,什么也不做
while operation!=6:
print('=' * 30)
print('+', end='')
print(' ' * 7, end='')
print('学生信息管理系统', end='')
print(' ' * 8, end='')
print('+')
print('+', end='')
print(' ' * 10, end='')
print('1.查看学生', end='')
print(' ' * 10, end='')
print('+')
print('+', end='')
print(' ' * 10, end='')
print('2.添加学生', end='')
print(' ' * 10, end='')
print('+')
print('+', end='')
print(' ' * 10, end='')
print('3.修改学生', end='')
print(' ' * 10, end='')
print('+')
print('+', end='')
print(' ' * 10, end='')
print('4.删除学生', end='')
print(' ' * 10, end='')
print('+')
print('+', end='')
print(' ' * 10, end='')
print('5.查找学生', end='')
print(' ' * 10, end='')
print('+')
print('+', end='')
print(' ' * 10, end='')
print('6.退出', end='')
print(' ' * 13, end='')
print('+')
print("=" * 30)
operation = input('请输入你的操作选择:')
try:
if isinstance(int(operation),int):
operation = int(operation)
while operation not in range(1,7):
operation = input('你输入的操作不存在,请重新输入:')
if isinstance(operation, int):
operation = int(operation)
operation_select(operation,username)
sleep(2)
else:
print('你输入的操作必须是(1~6)!!!')
sleep(2)
except:
print('程序异常!!!')
sleep(2)
print('返回上一级目录...')
def operation_select(operation:int,username:str):
'''
进入操作界面后具体的操作选择
:param operation: 操作选择
:return:
'''
if operation==1:
student.show_appoint_stus(username)
sleep(2)
elif operation==2:
one_student=student.add_student(username)
flag=file_operation.append_student(file_operation.get_all_students(),username,one_student)
print(flag)
sleep(2)
elif operation == 3:
stuname=input('请输入你要修改的学生的姓名:')
print(student.modify_student(stuname,username))
sleep(2)
elif operation == 4:
stuname=input('请输入你要删除的学生的姓名:')
print(student.del_student(stuname,username))
sleep(2)
elif operation == 5:
stuname=input('请输入你要查找的学生的姓名:')
student.find_student(stuname,username)
sleep(2)
def fun_operation(operation):
'''
操作方式选择
:param operation:
:return:
'''
operation = int(operation)
if operation == 1:
#进入登录界面
username=login.fun_login()
if username:
main_operation(username)
elif operation == 2:
#进入注册界面
logout.fun_regis()
elif operation == 3:
#退出登录、结束本程序
print('谢谢使用!!!')
exit(0)
if __name__=='__main__':
while True:
start_inter()
2.注册模块
"""__author__=wuliang"""
import file_operation
teacher={}
username=''
def fun_regis():
'''
注册功能实现
:return:
'''
while True:
username = input('请输入你的名字:')
if not file_operation.check_teacher(file_operation.get_all_teacher(),username):
while len(username)<3 or len(username)>16:
username = input('请重新输入你的名字(3~16位):')
print('该用户名可以使用!!!')
password = input('请输入你的密码:')
pw = input('确认密码:')
while pw != password:
print('密码错误,请重新输入!!')
password = input('请输入你的密码:')
pw = input('确认密码:')
teacher['teaname'] = username
teacher['password'] = pw
#将数据存入文件中
user_list = file_operation.get_all_teacher()
print(file_operation.append_teacher(user_list,teacher))
return
print('用户名已注册!!!')
# def pass_input():
# '''
# 进行密码确认
# :return:
# '''
# pw = ' '
# password=' '
#
# while pw==password:
# password = input('请输入你的密码:')
# pw = input('确认密码:')
# teacher['teaname'] = username
# teacher['password'] = pw
3.登录模块
"""__author__=wuliang"""
import file_operation
from time import sleep
def fun_login():
'''
教师登录功能的实现
:return: username
'''
username = input('请输入你的姓名:')
password = input('请输入你的密码:')
user_list = file_operation.get_all_teacher()
while True:
try:
if user_list.index({'teaname':username,'password':password})>=0:
print('登录成功,现在进入操作界面...')
sleep(1)
return username
except:
print('用户名或密码错误,请重新输入')
username = input('请输入你的姓名:')
password = input('请输入你的密码:')
4.文件操作模块
"""__author__=wuliang"""
import os
'''
文件操作模块,进行文件内容的查询,以及文件内容的更新
'''
try:
import cPickle as pickle
except ImportError:
import pickle
filename = 'user.txt'
student_file='student.txt'
def mkuserfile(filename):
'''
如果不存在建存储教师信息的文件并返回教师文件对象,否则返回教师文件对象
:return:文件对象
'''
fb = open(filename,'wb')
return fb
def get_onlyread(filename):
if not os.path.exists(filename):
mkuserfile(filename).close()
fb2 = open(filename, 'rb')
return fb2
def check_teacher(user_list,username):
'''
检查该姓名是否存在
:param username:
:return:
'''
file = get_onlyread(filename)
if os.path.getsize(filename):
for teacher in user_list:
if teacher['teaname']==username:
file.close()
return True
else:
file.close()
return False
file.close()
return False
def get_all_teacher():
'''
获取所有的教师
:return:
'''
file = get_onlyread(filename)
if os.path.getsize(filename):
user_list = pickle.load(file)
file.close()
return user_list
else:
file.close()
return []
def append_teacher(teacher_list:list,teacher:dict):
'''
添加教师
:param teacher_list:教师列表
:param teacher:新教师
:return:是否成功
'''
try:
file = mkuserfile(filename)
teacher_list.append(teacher)
pickle.dump(list(teacher_list),file)
file.close()
return '保存成功'
except:
return '保存失败'
def get_all_students():
'''
获取所有学生
:param username:
:return:
'''
#1.根据参数获取指定文件的内容
file = get_onlyread(student_file)
#2.建立学生总列表
stu_list = []
#3.判断文件是否为空
if os.path.getsize(student_file):
stu_list=pickle.load(file)
#4.关闭文件
file.close()
return stu_list
def append_student(student_list:list,username:str,student:dict):
'''
添加指定教师的学生
:param student_list:学生的总列表
:param username:教师姓名
:param student:新学生
:return:添加成功 or 失败
'''
if student_list==[]:
file = mkuserfile(student_file)
pickle.dump([[username,student]],file)
file.close()
return '添加成功'
for one_stu_list in student_list:
if one_stu_list[0]==username:
one_stu_list.append(student)
student_list[student_list.index(one_stu_list)]=one_stu_list
file = mkuserfile(student_file)
pickle.dump(student_list,file)
file.close()
return "添加成功"
file = mkuserfile(student_file)
student_list.append([username, student])
pickle.dump((student_list), file)
file.close()
return '添加成功'
def get_appoint_stu(username):
'''
获取指定教师的学生
:param username: 教师姓名
:return: stu_appoint_list
'''
stu_appoint_list=[]
all_list = get_all_students()
for s_l in all_list:
if s_l[0]==username:
stu_appoint_list=s_l
break
return stu_appoint_list
def check_student(username:str,stuname:str):
'''
检查是否该教师下是否已经有了那个学生
:param stuname: 学生姓名
:param username: 教师姓名
:return:False:名字已存在,True:可以注册
'''
stu_list=get_appoint_stu(username)
if stu_list:
for stu in stu_list[1:]:
if stu['stuname']==stuname:
return False
return True
def modify_student(student_list:list):
'''
删除学生并更新表
:param student_list: 已经删除了学生的的表
:return:True or False
'''
try:
file = mkuserfile(student_file)
pickle.dump(student_list, file)
file.close()
return True
except:
return False
def get_onestudnet(stuname:str,username:str):
'''
获取指定学生姓名的学生
:param stuname:
:param username:
:return:
'''
appoint_stu_list=get_appoint_stu(username)
for stu in appoint_stu_list[1:]:
if stu['stuname']==stuname:
return stu
return None
5.学生模块
"""__author__=wuliang"""
import file_operation
def add_student(username):
'''
添加学生,获取学生的输入
:return:
'''
stuname = input('请输入你要添加的学生的姓名:')
while not file_operation.check_student(username,stuname):
print('对不起输入的学生的姓名存在!!!')
stuname = input('请重新输入学生的姓名:')
print('该姓名可以注册!!!')
stuid = input('请输入你要添加的学号:')
phone_num = input('请输入你要添加的电话:')
age = input('请输入你要添加的年龄')
student={'stuname':stuname,'stuid':stuid,'phone_num':phone_num,'age':age}
return student
def show_appoint_stus(username:str):
'''
查看当前教师所有学生信息
:param username:
:return:
'''
stu_list=file_operation.get_appoint_stu(username)
for student_ in stu_list[1:]:
print('stuname:'+student_['stuname']+' stuid:'+student_['stuid'],end=' ')
print('phone_num:' + student_['phone_num'] + ' age:' + student_['age'])
def modify_student(stuname:str,username:str):
'''
:param stuname:
:param username:
:return:
'''
stu_appoint_list = file_operation.get_appoint_stu(username)
for stu in stu_appoint_list[1:]:
if stu['stuname']==stuname:
print('你要修改的学生存在!!')
new_stuname=input('请输入新的姓名:')
while not file_operation.check_student(username,new_stuname):
print('你输入的名字已存在!!!')
new_stuname=input('请重新输入姓名:')
new_stuid = input('请输入新的学号:')
new_phone_num = input('请输入新的电话号码:')
new_age = input('请输入新的年龄:')
student_={'stuname':new_stuname,'stuid':new_stuid,'phone_num':new_phone_num,'age':new_age}
all_stu_list=file_operation.get_all_students()
all_stu_list.remove(stu_appoint_list)
stu_appoint_list.remove(stu)
stu_appoint_list.append(student_)
all_stu_list.append(stu_appoint_list)
file_operation.modify_student(all_stu_list)
return '修改成功'
def del_student(stuname:str,username:str):
'''
:param stuname:
:param username:
:return:
'''
stu_all_list=file_operation.get_all_students()
stu_appoint_list=file_operation.get_appoint_stu(username)
stu_all_list.remove(stu_appoint_list)
for stu in stu_appoint_list[1:]:
if stu['stuname']==stuname:
stu_appoint_list.remove(stu)
stu_all_list.append(stu_appoint_list)
file_operation.dmodify_student(stu_all_list)
return '删除成功'
break
print('你要删除的学生不存在')
return '删除失败!!'
def find_student(stuname:str,username:str):
flag=file_operation.get_onestudnet(stuname,username)
if flag:
print('stuname:'+flag['stuname']+' stuid:'+flag['stuid'],end=' ')
print('stuname:'+flag['phone_num']+' age:'+flag['age'])
else:
print('对不起,你输入的学生不存在')
6.文件设计
user.txt:存储教师信息文件
存储格式:
[{'teaname':教师名,'password':登录密码}]
student.txt:存储学生信息文件
存储格式:
[[教师名,{'stuname':学生名,'stuid':学号,'phone_num':学生电话号码,'age':年龄},...],...]