學生信息管理系統
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號被刪除了,文件內不存在,並且以學號排序保存):
再次運行程序,查看文件內的信息: