StudentManagementSystem(Advance).py
'''
@Author: Kris Shin
@Date: 2018-07-23 15:48:12
@Last Modified by: Kris Shin
@Last Modified time: 2018-07-23 15:48:12
'''
import Management as mg # 导入Management模块
def main(): # 主函数
while True:
ex = mg.mainSlc() # 主操作界面
if not ex:
return -1 # 结束并退出
if __name__ == '__main__':
main()
Management.py
因为老师和学生的字典都是存在列表里面的,为了避免修改的时候一直找对应得索引,所以一开始将登录得老师的信息读出来之后就存进一个全局字典,便于操作,然后把原本的字典删除,在最后操作完之后再添加进去
'''
@Author: Kris Shin
@Date: 2018-07-27 18:48:47
@Last Modified by: Kris Shin
@Last Modified time: 2018-07-27 18:48:47
'''
import random # 随机生成id要用
import os
import json
import time
# 声明文件路径
filePath = os.path.join(os.path.join(os.getcwd(), 'StuManageListCopy'), 'AllInfo.json')
teachList = [] # 空老师列表,存所有老师字典
try:
with open(filePath) as teachf:
teachList = json.load(teachf) # 从文件中读取所有信息,存进老师列表
except Exception: # 出现异常则不进行操作
pass
# 定义所有的键 便于后面提示
keyTeAc = 'account'
keyTePd = 'password'
keyTeStu = 'students'
keyStuId = 'sId'
keyStuName = 'name'
keyStuAge = 'age'
keyStuGe = 'gender'
keyStuTel = 'tel'
keyStuSco = 'scores'
keyScoMt = 'Math'
keyScoEg = 'English'
keyScoPy = 'Python'
def loginView(): # 登陆界面
print('''
*******************************************************
*
* welcome to use Students Manage System
* 1.login
* 2.regist
* 0.exit
*
*******************************************************
''')
def mainView(): # 定义主界面
print('''
*******************************************************
* 1. add student
* 2. seek student
* 3. update student
* 4. delete student
* 5. get average grade
* 0. exit
*******************************************************
''')
def randomId(): # 定义随机id生成方法
id = 'Py1805'
for _ in range(4):
id += str(random.randint(0, 9))
return id
def ctnAndExView(): # 将继续操作的界面和方法封装
print('1.Continue\nPress any key to back')
return int(input('>>> ')) # 返回输入的值
def regist(): # 注册函数
account = input('Input account:')
for teach in teachList: # 遍历账号是否已经注册过
if account == teach[keyTeAc]:
print('Regist failed, Account is existed, please use another name')
return False
teach = {} # 把新注册的老师字典存进来
password = input('Input password:')
cfmpwd = input('confirm your password:')
if password != cfmpwd:
print('Regist failed, confirm password is not same as password')
return False
teach[keyTeAc] = account
teach[keyTePd] = password
teach[keyTeStu] = [] # 给老师一个空的学生列表
print('Teacher: ' + account + ' regist success!!')
teachList.append(teach) # 将老师字典添加进老师列表
def login(): # 登陆函数
account = input('Input account:')
for teach in teachList:
if account == teach[keyTeAc]: # 判断账户名是否正确
for _ in range(3): # 如果账户名正确有3次机会输入密码
password = input('Input password:')
if password == teach[keyTePd]:
global teacher # 如果密码正确则声明一个全局老师对象,只对已登录的老师字典进行操作
teacher = teach
teachList.remove(teach) # 删除原本的老师字典
print('welcome %s tercher' % account)
return True
else:
print('Wrong password!!!')
print('Login failed')
return False
print('Account is not exist, please regist first') # 如果账号不存在则给出提示
def welcome(): # 欢迎函数
loginView()
chos = input('>>> ')
if chos == '1':
if login():
return True # 如果登录失败则退出到循环中重新登陆,登陆成功则跳出循环
if chos == '2':
regist()
if chos == '0':
exit()
def addView(): # 添加学生功能实现
print('* add student')
student = {} # 存学生的字典
student[keyStuId] = randomId() # 调用函数生成id
student[keyStuName] = input('input name:\n>>> ')
student[keyStuAge] = int(input('input age:\n>>> '))
student[keyStuGe] = input('input gender:\n>>> ')
student[keyStuTel] = input('input tell:\n>>> ')
scores = {} # 学生的成绩字典
scores[keyScoMt] = int(input('Math:\n>>> '))
scores[keyScoEg] = int(input('English:\n>>> '))
scores[keyScoPy] = int(input('Python:\n>>> ')) # 修改字典的成绩值
student[keyStuSco] = scores
print('学生:%s ,id:%s 添加成功' % (student[keyStuName], student[keyStuId]))
return student # 返回student字典
def addStu(): # 添加学生方法
stuList = teacher[keyTeStu] # 读取老师原本的学生列表
student = addView() # 接收添加学生函数返回的学生字典
stuList.append(student) # 调用添加功能实现,将返回的列表对象存入老师的学生列表
teacher[keyTeStu] = stuList # 更新老师的学生列表
def showAllStu(stuList): # 定义显示所有学生信息的方法
print('ID\t\tname\tage\tgender\ttel\tMath\tEnglish\tPython')
for x in stuList: # 直接遍历学生信息,直接输出x,类型和标题无法对齐,所以取值进行输出
id, name, age, gender, tel, scores = x[keyStuId], x[keyStuName], x[keyStuAge], x[keyStuGe], x[keyStuTel], x[keyStuSco]
Math, English, Python = scores[keyScoMt], scores[keyScoEg], scores[keyScoPy]
print('%s\t%s\t%d\t%s\t%s\t%d\t%d\t%d' % (id, name, age, gender, tel, Math, English, Python))
def searchByStuName(stuList): # 查找所有学生函数
flag = False # 声明标识变量,标识是否查找到学生
name = input('input name:\n>>> ')
for stu in stuList:
if name in stu[keyStuName]:
flag = True # 查找到学生则赋值该标识为True
print(stu) # 输出学生信息
if not flag: # 如果没查找到学生则给出提示并终止查找函数
print('There are no student name as that')
return ""
return name # 返回输入的姓名,便于其他函数调用查找学生的姓名
def searchStu(): # 定义查找方法
print('* seek student')
stuList = teacher[keyTeStu]
showAllStu(stuList) # 调用查找所有学生函数
searchByStuName(stuList)
def updateStu(): # 定义更新学生函数
stuList = teacher[keyTeStu]
if not searchByStuName(stuList): # 调用查找学生函数
return False
print('* 3. update student')
id = input('input ID:\n>>> ') # 得到要修改的学生的id
for s in stuList:
if id == s[keyStuId]:
stuList.remove(s) # 如果找到了对应的学生 则删除原本的学生字典
s[keyStuId] = id
s[keyStuName] = input('input name:\n>>> ')
s[keyStuAge] = int(input('input age:\n>>> '))
s[keyStuGe] = input('input gender:\n>>> ')
s[keyStuTel] = input('input tel:\n>>> ')
s[keyStuSco]['Math'] = int(input('Math:\n>>> '))
s[keyStuSco]['English'] = int(input('English:\n>>> '))
s[keyStuSco]['Python'] = int(input('Python:\n>>> '))
stuList.append(s) # 通过index方法找到该id的学生的索引 并将更新后的stu对象赋值到allStu来更新学生信息
print('operation success!!')
teacher[keyTeStu] = stuList
return True # 返回-1 表示该函数结束
print('Update failed,the ID is not exist') # 如果上面查找到id那么这一步不会执行 否则提示无此id
def deleteStu(): # 定义删除学生方法
print('* delete student')
stuList = teacher[keyTeStu]
name = searchByStuName(stuList) # 调用查找学生方法 并得到返回的name
if not name: # 如果没有找到对应的学生则终止该函数
return False
cho = int(input(
'1.Delete all\n2.Delete by ID\n>>> ')) # 提示删除的类型(对有重名的学生的情况的判断和处理)
if cho == 2:
id = input('Input ID:\n>>> ')
for x in stuList:
if id == x[keyStuId]:
stuList.remove(x) # 通过输入id,来找到该学生字典,并删除
print('Delete success!!')
teacher[keyTeStu] = stuList
return -1
elif cho == 1: # 全部删除
for _ in range(len(stuList)): # 增加一次循环 因为通过name索引只能删除同名学生的第一个,需要重复操作
for x in stuList:
if name == x[keyStuName]:
stuList.remove(x) # 通过之前接收的查找学生的name来查找索引并删除
print('Delete success!!')
teacher[keyTeStu] = stuList
return -1
print('Delete failed,the ID is not exist') # 判断编号不存在的情况
def getAvgSco(): # 定义查看平均成绩
stuList = teacher[keyTeStu]
print('* get average grade')
print("ID\t\tname\tage\tgender\ttel\tavg")
for x in stuList: # 直接将最后一项做平均数计算操作
id, name, age, gender, tel, scores = x[keyStuId], x[keyStuName], x[keyStuAge], x[keyStuGe], x[keyStuTel], x[keyStuSco]
Math, English, Python = scores[keyScoMt], scores[keyScoEg], scores[keyScoPy]
print('%s\t%s\t%d\t%s\t%s\t%.2f' % (id, name, age, gender, tel, (Math + English + Python) / len(scores)))
def mainSlc(): # 主操作函数
while True:
if welcome():
break
while True: # 便于循环操作
mainView() # 调用主界面
slc = int(input('Selection:\n>>> '))
if slc == 1: # 判断选择 并进行相应操作
while True: # 便于重复操作
addStu()
select = ctnAndExView() # 判断是否继续操作
if select != 1: # 选择为0 返回主循环
break
if slc == 2:
while True:
searchStu()
select = ctnAndExView() # 判断是否继续操作
if select != 1: # 选择为0 返回主循环
break
if slc == 3:
while True:
updateStu()
select = ctnAndExView() # 判断是否继续操作
if select != 1: # 选择为0 返回主循环
break
if slc == 4:
while True:
deleteStu()
select = ctnAndExView() # 判断是否继续操作
if select != 1: # 选择为0 返回主循环
break
if slc == 5:
getAvgSco()
input('press anykey to return')
if slc == 0:
print('exiting......')
with open(filePath, 'w') as teachf:
teachList.append(teacher) # 把修改过的teacher重新添加到老师列表
json.dump(teachList, teachf) # 写入json文件
time.sleep(1) # 暂停一秒钟
return False # 返回False 结束主循环 退出系统
time.sleep(1) # 每次操作完成暂停一秒钟
os.system('cls') # 清屏