赞
踩
import logging
import hashlib
import shelve
from random import randint
class student():
def init(self, id, yw, sx, en):
self.id = id
self.yw = yw
self.sx = sx
self.en = en
def __str__(self):
return f"学号:{self.id}\n总分:{self.total()}\n"
def total(self):
return self.yw + self.sx + self.en
def pm(self, alist):
return len(list(filter(lambda i: i.total() > self.total(), alist))) + 1
user = ‘’
datas = []
accouts = {“admin”: {“pwd”: “21232f297a57a5a743894a0e4a801fc3”, “role”: “1234567890”},
“zhangsan”: {“pwd”: “e10adc3949ba59abbe56e057f20f883e”, “role”: “12360”},
“1”: {“pwd”: “c4ca4238a0b923820dcc509a6f75849b”, “role”: “1234567890”}}
funcdic = {“1”: “打印出原始数据”,
“2”: “打印出成绩排名”,
“3”: “打印出成绩统计”,
“4”: “列出补考名单”,
“5”: “增加学生记录”,
“6”: “查询学生记录”,
“7”: “修改学生记录”,
“8”: “删除学生记录”,
“9”: “用户管理”,
“0”: “退出!”}
subjectdic = {“语文”: 0, “数学”: 1, “英语”: 2}
logging.basicConfig(level=logging.DEBUG,
filename=“xxgl.log”,
format=“%(asctime)s %(filename)s : %(levelname)s %(message)s”,
datefmt=“%Y-%m-%d %H:%M:%S”,
filemode=“a”)
def init():
global datas, accouts
with shelve.open(“mydb”) as f:
try:
datas = f[“data”]
accouts = f[“user”]
logging.info(“从数据库读入信息成功!”)
except:
f[“data”] = datas
f[“user”] = accouts
logging.warning(“数据库恢复到初始值!”)
def login():
global user
uname = input(“请输入用户名:”)
passwd = input(“请输入密码:”)
password = hashlib.md5(passwd.encode()).hexdigest()
if uname in accouts:
if password == accouts[uname][“pwd”]:
logging.info(f"用户{uname}登录成功!“)
user = uname
return
print(“用户名或密码错!”)
logging.warning(f"用户{uname}登录失败!”)
def save():
with shelve.open(“mydb”) as f:
f[“data”] = datas
f[“user”] = accouts
def printdata(f, title):
table = sorted(datas, key=f)
print(f"{title:-^70s}“)
print(f’{“学号”:10s}{“语文”:10s}{“数学”:10s}{“英语”:10s}{“总分”:10s}{“名次”:10s}')
print(”-" * 75)
for i in table:
print(f’{i.id:12s}{i.yw:12d}{i.sx:12d}{i.en:12d}{i.total():12d}{i.pm(datas):12d}')
print(“-” * 75)
logging.info(f"{user}打印了{title}!")
def add1():
print(“添加学生成绩::”.center(65, ‘-’))
global datas
b = [i.id for i in datas]
while 1:
id = input("请输入学号:").strip()
if id not in b:
break
print('该生信息已存在,请重新输入!')
yw = int(input("语文:").strip())
sx = int(input("数学:").strip())
en = int(input("英语:").strip())
datas.append(student(id, yw, sx, en))
save()
logging.info(f"{user}增加了一条学生记录,学号{id}。")
def datafx():
yw = [i.yw for i in datas]
sx = [i.sx for i in datas]
en = [i.en for i in datas]
print(“语文成绩统计”.center(50, “-”))
print(f"平均分:{sum(yw) / len(yw):40.2f}“)
print(f"及格率:{100 * sum([i >= 60 for i in yw]) / len(yw):40.2f}%”)
print(f"优秀率:{100 * sum([i >= 80 for i in yw]) / len(yw):40.2f}%\n")
print(“数学成绩统计”.center(50, “-”))
print(f"平均分:{sum(sx) / len(sx):40.2f}“)
print(f"及格率:{100 * sum([i >= 60 for i in sx]) / len(sx):40.2f}%”)
print(f"优秀率:{100 * sum([i >= 80 for i in sx]) / len(sx):40.2f}%\n")
print(“英语成绩统计”.center(50, “-”))
print(f"平均分:{sum(en) / len(en):40.2f}“)
print(f"及格率:{100 * sum([i >= 60 for i in en]) / len(en):40.2f}%”)
print(f"优秀率:{100 * sum([i >= 80 for i in en]) / len(en):40.2f}%\n")
logging.info(f"{user}查看了成绩分析。")
def query():
print(“查看学员成绩:”.center(65, ‘-’))
b = [i.id for i in datas]
while 1:
id = input(“请输入学号:”)
if id in b:
break
print(‘该生信息不存在,请重新输入!’)
a = list(filter(lambda i: i.id == id, datas))[0]
print(f"语文成绩:{a.yw}“)
print(f"数学成绩:{a.sx}”)
print(f"英语成绩:{a.en}\n")
logging.info(f"{user}查看了学员{id}的成绩。")
def databk():
ywb = []
sxb = []
enb = []
for k in datas:
if k.yw < 60:
ywb.append([k.id, k.yw])
if k.sx < 60:
sxb.append([k.id, k.sx])
if k.en < 60:
enb.append([k.id, k.en])
print(“语文补考名单:”.center(65, ‘-’))
print(f’{“学号”:8s}{“分数”:18s}‘)
for i in ywb:
print(f"{i[0]:^10s} {i[1]:^20d}\n")
print(“数学补考名单:”.center(65, ‘-’))
print(f’{“学号”:8s}{“分数”:18s}‘)
for i in sxb:
print(f"{i[0]:^10s} {i[1]:^20d}\n")
print(“英语补考名单:”.center(65, ‘-’))
print(f’{“学号”:8s}{“分数”:18s}')
for i in enb:
print(f"{i[0]:^10s} {i[1]:^20d}\n")
logging.info(f"{user}查看了补考名单。")
def update1():
print(“修改学生成绩:”.center(65, ‘-’))
b = [i.id for i in datas]
while 1:
id = input(“请输入学号:”)
if id in b:
break
print(‘该生信息不存在,请重新输入!’)
a = list(filter(lambda i: i.id == id, datas))[0]
s1 = int(input(“语文成绩:”))
s2 = int(input(“数学成绩:”))
s3 = int(input(“英语成绩:”))
a.yw = s1
a.sx = s2
a.en = s3
save()
print(‘修改成功’)
logging.info(f"{user}修改了学生{id}的成绩。")
def delete():
print(“删除学员:”.center(65, ‘-’))
global datas
b = [i.id for i in datas]
while 1:
id = input(“请输入学号:”)
if id in b:
break
print(‘该生信息不存在,请重新输入!’)
a = list(filter(lambda i: i.id != id, datas))
datas = a
save()
logging.info(f"{user}删除了学员{id}。")
def manage():
global accouts
print(“功能列表:”)
while 1:
while 1:
print(“用户管理功能”.center(70, ‘-’))
op = input(“1增加用户 2删除用户 3修改密码权限 4查询用户 5退出 \n”).strip()
if op in list(“12345”):
break
print(“输入有误!”)
if op == “1”: # 添加用户
print(f"{‘添加用户’:-^60s}“)
uname = input(“用户名:”).strip()
pd = input(“密码:”).strip()
pd = hashlib.md5(pd.encode()).hexdigest()
ro = input(“权限:”).strip()
if uname in accouts:
print(“用户已存在!”)
else:
accouts[uname] = {“pwd”: pd, “role”: ro}
logging.warning(f”{user}新建了账户{uname}!“)
save()
elif op == “2”: # 删除账户
print(f”{‘删除账户’:-^60s}“)
uname = input(“用户名:”).strip()
if uname in accouts:
del accouts[uname]
save()
logging.warning(f”{user}删除了账户{uname}!“)
else:
print(“用户名不存在!”)
elif op == “3”: # 修改密码权限
print(f”{‘修改密码权限’:-^60s}“)
while 1:
uname = input(“请输入用户名”).strip()
if uname not in accouts:
print(‘您输入的用户不存在,请重新输入’)
else:
break
pwd1 = input(“新密码:”)
pwd2 = input(“确认密码:”)
if pwd1 == pwd2:
pd = hashlib.md5(pwd1.encode()).hexdigest()
accouts[uname][“pwd”] = pd
save()
logging.warning(f”{user}修改了{uname}账户密码!“)
else:
print(“两次密码不一致!”)
quanxian = input(“请输入权限:”)
accouts[uname][‘role’] = quanxian
save()
logging.warning(f”{user}修改了{uname}账户权限!“)
elif op == “4”: # 查询用户信息
print(f”{‘查询用户信息’:-^60s}“)
print(f”{‘用户名’:10s}{‘密码’:35s}{‘权限’:^10s}“)
for k, v in accouts.items():
print(f”{k:13s}{v[‘pwd’]:37s}{v[‘role’]:^12s}“)
print()
logging.warning(f”{user}查询了账户信息!")
else:
break
def main():
init()
login()
if user in accouts:
role = accouts[user][“role”]
print(“欢迎使用天融信学生成绩管理系统”.center(60, “-”))
print()
print(“作者:wf 版本:终版\n”.center(65))
while 1:
print(“功能列表:”)
for i in role:
print(f"{i:<5s}{funcdic[i]}“)
# 判断输入合法性
while 1:
select = input(“你的选择是:”).strip()
if select in role:
break
print(“输入有误,请选择功能编号!”)
if select == “1”: # “1”:“打印出原始数据”
printdata(lambda i: i.id, “原始成绩表”)
elif select == “2”: # “2”:“打印出成绩排名”
printdata(lambda i: i.pm(datas), “排名成绩表”)
elif select == “3”: # “3”:“打印出成绩统计”
datafx()
elif select == “4”: # “4”:“列出补考名单”,
databk()
elif select == “5”: # “5”:“增加学生记录”,
add1()
elif select == “6”: # “6”:“查询学生记录”,
query()
elif select == “7”: # “7”:“修改学生记录”,
update1()
elif select == “8”: # “8”:“删除学生记录”,
delete()
elif select == “9”: # “9”:“用户管理”,
manage()
else:
logging.info(f”{user}退出了系统。")
exit()
if name == “main”:
main()
init()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。