import json
import os
import pprint
class StudentInfo(object):
data = {
'000': {
'学号': '000',
'姓名': 'liyu',
'年龄': '18',
'性别': 'male',
'出生年月': '1999-05',
'地址': '西安',
'电话': '12345678',
'E-mail': '[email protected]'
}
}
def __init__(self):
self.info = {}
self.newData = {}
@staticmethod
def mainMenu():
print('学生信息管理系统'.center(50, '#'))
print("""
1.录入信息
2.查询信息
3.退出
请选择:
""")
key = input()
return key
def addInfo(self):
id = input('请输入学号:')
if id not in self.data:
name = input('请输入姓名(拼音):')
age = input('请输入年龄:')
sex = input('请输入性别(male,female):')
dateOfBirth = input('请输入出生年月:')
address = input('请输入地址:')
phoneNum = input('请输入电话:')
Emale = input('请输入电子邮箱:')
self.data[id] = {'学号': id,
'姓名': name,
'年龄': age,
'性别': sex,
'出生年月': dateOfBirth,
'地址': address,
'电话': phoneNum,
'E-mail': Emale
}
print('学生%s添加成功!' % name)
else:
print('学号为%s的学生信息已存在!' % id)
def adminMenu(self):
key = input("""
1.按学号排序
2.按姓名排序
3.返回上一层
请选择
""")
return key
def idSort(self):
print('按学号排序结果如下:')
pprint.pprint(self.data)
def nameSort(self):
userInfo = self.data.values()
for j in userInfo:
self.newData[j['姓名']] = j
pprint.pprint(self.newData)
def commonUser(self, userName, userId):
print('您的信息如下:')
self.info[userName] = self.data[userId]
pprint.pprint(self.info)
self.info = {}
def queryMenu(self):
userName = input("""
请输入您的姓名:
""")
return userName
def main():
while True:
key = s.mainMenu()
if key == '1':
s.addInfo()
elif key == '2':
userName = s.queryMenu()
if userName == 'admin':
while True:
key = s.adminMenu()
if key == '1':
s.idSort()
elif key == '2':
s.nameSort()
elif key == '3':
break
else:
print('输入有误!')
else:
for userId, userInfo in s.data.items():
if userName == userInfo.get('姓名'):
s.commonUser(userName, userId)
break
else:
print('不存在%s的信息' % userName)
elif key == '3':
break
else:
print('输入有误!')
if __name__ == '__main__':
s = StudentInfo()
if os.path.exists('studentInfo.txt'):
with open('studentInfo.txt', 'r') as f:
data = f.read()
s.data = json.loads(data)
main()
dataStr = json.dumps(s.data)
with open('studentInfo.txt', 'w') as f:
f.write(dataStr)
else:
with open('studentInfo.txt', 'w+') as f:
f.seek(0, 0)
main()
dataStr = json.dumps(s.data)
f.write(dataStr)
# encoding=utf-8
"""
Date:2019-06-27 09:11
User:LiYu
Email:[email protected]
"""
import json
import os
import pprint
class StudentInfo(object):
data = {
'000': {
'id': '000',
'name': 'liyu',
'sex': 'male',
'addr': '西安',
'phoneNum': 12345678,
'chineseScore': 100,
'mathScore': 100,
'englishScore': 100,
'averScore': '100.00',
'examGrade': 1,
'classmateScore': 100,
'moralityScore': 100,
'teacherScore': 100,
'allScore': '100.00',
'allGrade': 1,
}
}
def __init__(self):
self.info = {}
self.newData = {}
@staticmethod
def mainMenu():
print('学生综合评测系统'.center(50, '#'))
print("""
1.录入(修改)信息
2.查询信息
3.删除信息
4.退出
请选择:
""")
key = input()
return key
def addInfo(self):
averScoreDict = {}
allScoreDict = {}
id = input('请输入学号:')
name = input('请输入姓名(拼音):')
sex = input('请输入性别(male,female):')
address = input('请输入地址:')
phoneNum = input('请输入电话:')
chineseScore = int(input('请输入语文成绩:'))
mathScore = int(input('请输入数学成绩:'))
englishScore = int(input('请输入英语成绩:'))
averScore = '%.2f' % ((chineseScore + mathScore + englishScore) / 3)
classmateScore = int(input('请输入同学互评分:'))
moralityScore = int(input('请输入品德成绩:'))
teacherScore = int(input('请输入教师评分:'))
allScore = '%.2f' % \
(float(averScore) * 0.6 +
classmateScore * 0.1 +
moralityScore * 0.1 +
teacherScore * 0.2)
self.data[id] = {'id': id,
'name': name,
'sex': sex,
'addr': address,
'phoneNum': phoneNum,
'chineseScore': chineseScore,
'mathScore': mathScore,
'englishScore': englishScore,
'averScore': averScore,
'examGrade': 0,
'classmateScore': classmateScore,
'moralityScore': moralityScore,
'teacherScore': teacherScore,
'allScore': allScore,
'allGrade': 0,
}
# 求考试平均成绩和总成绩排名
for i in self.data:
# {'学号1':考试平均成绩1, '学号2':考试平均成绩2......}
averScoreDict[self.data.get(i).get('id')] = float(self.data.get(i).get('averScore'))
# {'学号1':总成绩1, '学号2':总成绩2......}
allScoreDict[self.data.get(i).get('id')] = float(self.data.get(i).get('allScore'))
# [('学号1', 考试平均成绩1), ('学号2', 考试平均成绩2)...] 按成绩降序排列
averScoreSort = sorted(zip(averScoreDict.keys(), averScoreDict.values()), key=lambda x: x[1], reverse=True)
# [('学号1', 总成绩1), ('学号2', 总成绩2)...] 按成绩降序排列
allScoreSort = sorted(zip(allScoreDict.keys(), allScoreDict.values()), key=lambda x: x[1], reverse=True)
# 写入考试平均成绩排名
for i in averScoreSort:
flag = averScoreSort.index(i) + 1
self.data[i[0]]['examGrade'] = flag
# 写入总成绩排名
for i in allScoreSort:
flag = allScoreSort.index(i) + 1
self.data[i[0]]['allGrade'] = flag
print('学生%s添加(更新)成功!' % name)
# pprint.pprint(self.data)
def adminMenu(self):
key = input("""
欢迎管理员使用,您可以浏览所有学生信息:
1.按学号排序
2.按姓名排序
3.返回上一层
请选择:
""")
return key
def idSort(self):
print('按学号排序结果如下:')
pprint.pprint(self.data)
def nameSort(self):
userInfo = self.data.values()
for j in userInfo:
self.newData[j['name']] = j
print('按姓名排序结果如下:')
pprint.pprint(self.newData)
def commonUser(self, userName, userId):
print('您的信息如下:')
self.info[userName] = self.data[userId]
pprint.pprint(self.info)
self.info = {}
def queryMenu(self):
userName = input("""
请输入您的姓名:
""")
return userName
def delInfo(self):
delId = input('请输入想要删除的学号:')
if delId in self.data:
print('您想要删除的信息如下:')
pprint.pprint(self.data[delId])
ensure = input('确定删除吗?(y/n)')
if ensure == 'y':
del self.data[delId]
print('学号为%s的学生信息删除成功!' % delId)
else:
print('请重新选择您需要的服务!')
else:
print('不存在学号为%s的学生信息!' % delId)
def main():
while True:
key = s.mainMenu()
if key == '1':
s.addInfo()
elif key == '2':
userName = s.queryMenu()
if userName == 'admin':
while True:
key = s.adminMenu()
if key == '1':
s.idSort()
elif key == '2':
s.nameSort()
elif key == '3':
break
else:
print('输入有误!')
else:
for userId, userInfo in s.data.items():
if userName == userInfo.get('name'):
s.commonUser(userName, userId)
break
else:
print('不存在%s的信息' % userName)
elif key == '3':
s.delInfo()
elif key == '4':
break
else:
print('输入有误!')
if __name__ == '__main__':
s = StudentInfo()
if os.path.exists('studentScoreInfo.txt'):
with open('studentScoreInfo.txt', 'r') as f:
data = f.read()
s.data = json.loads(data)
main()
dataStr = json.dumps(s.data)
with open('studentScoreInfo.txt', 'w') as f:
f.write(dataStr)
else:
with open('studentScoreInfo.txt', 'w+') as f:
f.seek(0, 0)
main()
dataStr = json.dumps(s.data)
f.write(dataStr)
添加(更新)信息:
查询信息(输入姓名查询)
如果输入的是管理员姓名,可浏览所有学生信息(可选择学号排序或姓名排序):
删除信息:
删除之后再次查询,不存在该信息:
查看文件(默认的000号被删除了,文件内不存在,并且以学号排序保存):
再次运行程序,查看文件内的信息: