下面是存储版的,利用笔记三中的本地文件的创建、读取和写入来完成学员管理系统的增删改查!还用到了异常捕获来消除一部分bug!如有错误或不解,请在评论区指教!
def read_data():
# 读取本地文件
# return:None
# 1.打开文件
file= open('stu.txt','r')
# 2.读取数据
result = file.readlines()
# 3.去除姓名中的\n,并且去除后的姓名存在stu中
for name in result:
# 去除\n
name = name.strip('\n')
# 存储stu大列表中
stu_inf.append(name)
# 关闭文件
file.close()
def save_data():
# 保存数据到本地文件
# 1.打开文件
file = open('stu.txt','w')
# 2.遍历每一个学员姓名 写入姓名后 再写换行符
for name in stu_inf:
file.write(name)
file.write('\n')
file.close()
def show():
print('-------------------------学员管理系统(1.0版本)-------------------------')
print('-----------------------------1.添加学员信息------------------------------')
print('-----------------------------2.修改学员信息------------------------------')
print('-----------------------------3.删除学员信息------------------------------')
print('-----------------------------4.查询所有学员------------------------------')
print('-----------------------------0.退出系统----------------------------------')
def add():
# 添加学员信息
while True:
print('1.追加学员信息')
print('2.插入学员信息')
print('输入q退出')
add_choose = input('请选择:')
if add_choose == 'q':
break
# 判断输入的选择是否为int类型
try:
add_choose = int(add_choose)
except:
print('输入有误,请重新输入!')
continue
else:
while True:
# 判断输入的选择是否在范围
if add_choose < 1 or add_choose > 2:
print('您输入的选择不在范围,请重新输入!')
break
else:
if add_choose == 1:
while True:
inf = input('请输入学员信息(退出追加信息请输入q):')
if inf == 'q':
break
stu_inf.append(inf)
print('添加成功!')
else:
while True:
insert_index = input('请输入要插入的索引号(退出请输入q):')
if insert_index == 'q':
break
# 判断输入的索引是否为int类型
try:
insert_index = int(insert_index)
except:
print('输入有误,请重新输入要插入的索引号:')
continue
else:
while True:
# 判断输入的索引是否在范围
if (insert_index < 0) or (insert_index >= len(stu_inf)):
print('您输入的索引号不存在,请重新输入要插入的索引号:')
break
else:
insert_information = input('请输入要插入的学员信息:')
stu_inf.insert(insert_index, insert_information)
print('添加成功!')
break
break
break
def query():
# 查询所有学员信息
for x in range(0, len(stu_inf)):
information = stu_inf[x]
print('索引为:%s 学员信息为:%s' % (x + 1, information))
def modify():
# 修改学员信息
while True:
query()
modify_index = input('请输入要修改的索引(退出请输入q):')
if modify_index == 'q':
break
try:
modify_index = int(modify_index)
except:
print('输入有误,请重新输入!')
else:
while True:
if (modify_index < 0) or (modify_index >= len(stu_inf)):
print('你输入的索引不存在,请重新输入要修改的索引:')
break
else:
modify_information = input('请输入要修改的内容:')
stu_inf[modify_index] = modify_information
print('修改成功!')
break
def delete():
# 删除学员信息
while True:
print('1.根据索引删除学员信息')
print('2.删除全部成员')
delete_choose = input('请选择(退出请输入q):')
if delete_choose == 'q':
break
try:
delete_choose = int(delete_choose)
except:
print('您输入有误,请重新选择!')
continue
else:
while True:
# 判断输入的选择是否在范围
if delete_choose < 1 or delete_choose > 2:
print('您输入的选择不在范围,请重新输入!')
break
else:
if delete_choose == 1:
while True:
query()
delete_index = input('请输入需要删除信息的索引(退出请输入q):')
if delete_index == 'q':
break
try:
delete_index = int(delete_index)
except:
print('输入有误,请重新输入!')
continue
else:
while True:
if (delete_index < 0) or (delete_index >= len(stu_inf)):
print('您输入的索引不存在,请重新输入!')
break
else:
del stu_inf[delete_index]
print('删除成功!')
break
break
else:
stu_inf.clear()
print('删除所有学员信息成功!')
break
stu_inf = []
read_data()
while True:
show()
choose = input('请选择:')
str_choose = choose.isdigit()
if str_choose:
choose = int(choose)
if choose == 1:
add()
save_data()
elif choose == 2:
modify()
save_data()
elif choose == 3:
delete()
save_data()
elif choose == 4:
query()
save_data()
elif choose == 0:
print('欢迎下次使用!')
break
else:
print('您的选项不存在,请重新输入!')
else:
print('您输入的不是数字!请重新选择!')