You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import os.path
filename = 'student.txt'
def menu():
print('======学生信息管理系统======')
print('1.输入学生信息')
print('2.查找信息')
print('3.删除信息')
print('4.修改信息')
print('5.显示出所有信息')
print('0.退出系统')
print('=========================')
def main():
while True:
menu()
selection = int(input('请选择:'))
if selection in [0, 1, 2, 3, 4, 5]:
if selection == 0:
ans = input('是否要退出y/n')
if ans == 'y' or ans == 'Y':
print('感谢使用!!')
break
else:
continue
elif selection == 1:
insert()
elif selection == 2:
search()
elif selection == 3:
delete()
elif selection == 4:
edit()
elif selection == 5:
display()
else:
print('选择错误')
# noinspection PyBroadException
def insert():
student_list = []
while True:
stu_id = input('请输入学号:')
name = input('请输入姓名:')
sex = input('请输入性别:')
phone = int(input('请输入电话号码:'))
student = {'stu_id': stu_id, 'name': name, 'sex': sex, 'phone': phone}
student_list.append(student)
ans = input('是否继续添加y/n\n')
if ans == 'y' or ans == 'Y':
continue
else:
break
save(student_list)
print('输入完毕!')
# noinspection PyBroadException
def save(lst):
try:
stu_txt = open(filename, 'a', encoding='utf-8')
except:
stu_txt = open(filename, 'w', encoding='utf-8')
for item in lst:
stu_txt.write(str(item) + '\n')
stu_txt.close()
def search():
stu_query = []
while True:
stu_id = ''
if os.path.exists(filename):
id = input('请输入学生学号:')
with open(filename, 'r', encoding='utf-8') as rfile:
student = rfile.readlines()
for item in student:
d = dict(eval(item))
if id != '':
if d['stu_id'] == id:
stu_query.append(d)
show_student(stu_query)
stu_query.clear()
ans = input('是否继续查询?y/n')
if ans == 'y':
continue
else:
break
else:
print('暂无学生信息')
return
def show_student(lst):
if len(lst) == 0:
print('没有查询到数据!')
return
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^15}\t'
print(format_title.format('ID', '姓名', '性别', '电话'))
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^15}\t'
for item in lst:
print(format_data.format(item.get('stu_id'),
item.get('name'),
item.get('sex'),
item.get('phone')
))
def delete():
while True:
stu_id = input('请输入要删除的学号:')
if stu_id != '':
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as file:
stu_old = file.readlines()
else:
stu_old = []
flag = False
if stu_old:
with open(filename, 'w', encoding='utf-8') as wfile:
d = {}
for item in stu_old:
d = dict(eval(item))
if d['stu_id'] != stu_id:
wfile.write(str(d) + '\n')
else:
flag = True
if flag:
print(f'id为{stu_id}的学生信息已经被删除')
else:
print(f'没有找到{stu_id}的学生信息')
else:
print('无学生信息')
break
display()
ans = input('是否继续删除y/n\n')
if ans == 'y' or ans == 'Y':
continue
else:
break
def edit():
display()
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
student_old = rfile.readlines()
else:
return
student_id = input('请输入要修改的学生学号:')
with open(filename, 'w', encoding='utf-8') as wfile:
for item in student_old:
d = dict(eval(item))
if d['stu_id'] == student_id:
print('已经找到学生信息,请修改:')
while True:
d['name'] = input('请输入姓名')
d['sex'] = input('请输入性别')
d['phone'] = input('请输入电话')
wfile.write(str(d) + '\n')
print('修改成功')
else:
wfile.write(str(d) + '\n')
ans = input('是否继续修改y/n')
if ans == 'y':
edit()
def display():
student_list = []
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
for item in students:
student_list.append(eval(item))
if student_list:
show_student(student_list)
else:
print('没有学生信息')
if 1 == 1:
main()