目前实现了以下功能:
添加学生
修改学生信息
删除学生
注册
import json
def showInfo(): # 显示功能列表
while True:
print("千峰学生管理系统V1.0")
print("1:addInfo")
print("2:delInfo")
print("3:modifyInfo")
print("4:regInfo")
print("0:quitInfo")
key = int(input('请输入0-4>>>'))
if key == 0:
quitInfo()
break
elif key == 1:
addInfo()
elif key == 2:
delInfo()
elif key == 3:
modifyInfo()
elif key == 4:
regInfo()
else:
print("错误,请重新输入")
def addInfo():
name = input("请输入姓名:")
idlist = input("请输入ID:")
age = input("请输入年龄:")
stuInfo = {}
stuInfo['name'] = name
stuInfo['ID'] = idlist
stuInfo['age'] = age
with open('x.json','r',encoding='utf-8') as f:
content = json.load(f)
content.append(stuInfo)
print(content)
with open('x.json','w',encoding='utf-8') as f:
json.dump(content,f)
print('添加%s成功' % name)
def delInfo():
delNum = input("请输入要删除的xm:")
with open('x.json','r',encoding='utf-8') as f:
content = json.load(f)
for x in content:
if x['name'] == delNum:
content.remove(x)
break
with open('x.json','w',encoding='utf-8') as f:
json.dump(content,f)
print('删除%s成功' % delNum)
def modifyInfo():
checkNum = input('name=')
flag = False
with open('x.json','r',encoding='utf-8') as f:
content = json.load(f)
for x in content:
if x['name'] == checkNum:
print(x)
flag = True
break
if flag:
print('please input your change:')
modifyNum =input("请输入姓名")
modifyid = input("请输入ID")
modifyage = input("请输入年龄")
x['name'] = modifyNum
x['ID'] =modifyid
x['age'] =modifyage
with open('x.json', 'w', encoding='utf-8') as f:
json.dump(content,f)
print('修改%s成功'% checkNum)
else:
print('there is not this student')
def regInfo():
number =1
name = input('请输入你的姓名:')
age = input('请输入你的年龄')
new_number = str(number)
idlist = 'py1805' + new_number.rjust(3, '0')
showInfo ={}
showInfo['name'] = name
showInfo['age'] = age
showInfo['ID'] = idlist
number += 1
with open('x.json', 'r', encoding='utf-8') as f:
content = json.load(f)
content.append(showInfo)
print(content)
with open('x.json', 'w', encoding='utf-8') as f:
json.dump(content, f)
print('注册成功')
def quitInfo():
exit()