马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
经过Twilight6等人的努力,我们开发出了学生查询2.0
废话少说,上代码:(注意,建议使用辅件)from time import sleep
from random import randint
data = [{}]
while True:
# 循环开头显示功能
print(" ****************************************")
print(" 欢迎使用学生管理系统v2.0(后续有3.0)")
print(" 1.显示所有学生")
print(" 2.新建学生档案")
print(" 3.查询学生信息")
print(" 4.修改学生档案")
print(" 5.删除学生档案")
print(" 6.提取学生数据")
print(" 0.退出")
print(" ****************************************")
print()
def print_all():
for student in data:
print(student)
pass
pass
def someThing_put_in():
ID = randint(1000000000, 9999999999)
try:
if ID in data:
ID = randint(1000000000, 9999999999)
except:
print("调试错误!")
someName = input("输入姓名:")
someSex = input("输入性别:")
someAddress = input("输入地址:")
student = {"ID": ID, "name": someName, "sex": someSex, "address": someAddress}
data.append(student)
def find_student():
print("为了确保您是管理员,请输入学生全名(非小名)")
somename = input()
try:
for i in data:
if i["name"] == somename:
print(i)
pass
pass
pass
except:
print("有错误")
def write_student():
someBody = input("请输入学生全名(非小名):")
for j in data:
if j["name"] == someBody:
TA_new_name = input("请输入新名字:")
TA_new_ID = randint(1000000000, 9999999999)
if TA_new_ID in data:
TA_new_ID = randint(1000000000, 9999999999)
else:
TA_new_sex = input("输入性别:")
TA_new_address = input("输入地址:")
NEW_student = {"ID": TA_new_ID, "name": TA_new_name, "sex": TA_new_sex, "address": TA_new_address}
data.append(NEW_student)
LAST_name = input("输入原先的名字:")
for k in data:
if k["name"] == LAST_name:
need_del = k
data.remove(need_del)
def del_student():
someGuy = input("请输入学生全名(非小名):")
for find in data:
if find["name"] == someGuy:
print()
sleep(1)
print("Find it!")
data.remove(find)
def save():
if data!= []:
with open('data.txt','w',encoding='utf-8') as f:
f.write('{:^10}{:^10}{:^10}{:^10}\n\n'.format('ID','Name','Sex','Address'))
for i in data:
f.write('{:^10}{:^10}{:^10}{:^10}\n\n'.format(i['ID'],i['name'],i['sex'],i['address']))
print('保存数据成功!')
else:
print('你的档案库为空!')
def extract():
global data
try:
with open('data.txt', encoding='utf-8') as f:
temp = []
for i in f:
if i[0].isdigit():
temp.append([each for each in i.split() if each != ''])
for i in temp:
temp_list = {}
temp_list['ID'] = int(i[0])
temp_list['name'] = i[1]
temp_list['sex'] = i[2]
temp_list['address'] = i[3]
if temp_list not in data:
data.append(temp_list)
print('提取学生档案成功!')
except OSError:
print('错误:档案文件<data.txt>不存在!')
someThing = input("Enter the num:")
if someThing == "0":
break
elif someThing == "1":
print_all()
continue
elif someThing == "2":
print("好的")
print()
sleep(1)
someThing_put_in()
elif someThing == "3":
print("OK")
print()
find_student()
elif someThing == "4":
print("OK")
sleep(1)
write_student()
elif someThing == "5":
print("OK")
print()
del_student()
elif someThing == "6":
more_Thing = input("要提取还是读取(填R(读取)或S(保存)):")
if more_Thing == "R":
extract()
elif more_Thing == "S":
save()
附件:
|