您好,登录后才能下订单哦!
import os
stus = [] # 学生信息列表(每个学生的信息以字段存储)
def add_stu():
sid = input('输入学生学号:')
sname = input('输入学生姓名:')
sage = input('输入学生年龄:')
dict_stu = {'id': sid.strip(), 'name': sname.strip(), 'age': int(sage.strip())}
stus.append(dict_stu)
def remove_stu(name):
item = search_stu(name)
stus.remove(item)
def search_stu(name):
flag = False
for s in stus:
if s['name'] == name.strip():
print(s)
flag = True
return s
if flag == False:
print('该学生不存在')
return None
def query_all_stu():
print('序号\tid\tname\tage')
for i, stu in enumerate(stus, 1):
print(i, end="")
print('\t%s\t%s\t%s' % (stu['id'], stu['name'], stu['age']))
def print_menu():
print('=' * 30)
print('学生管理系统'.center(30))
print('输入1:添加学生')
print('输入2:查找学生')
print('输入3:修改学生')
print('输入4:删除学生')
print('输入5:查看所有学生')
print('输入6:退出')
def file_read():
try:
file_name=r'stu.txt'
if os.path.exists(file_name):
f = open(file_name, 'r', encoding='utf-8')
while True:
line_text = f.readline()
if line_text == "":
break
line_list = line_text.split()
dict_item={'id':line_list[0],'name':line_list[1],'age':line_list[2]}
stus.append(dict_item)
print(line_list)
f.close()
else:
print('file is not exist.')
except Exception as e:
print(e)
finally:
print('file_read methon is exec completed.')
def file_write():
try:
file_name = r'stu.txt'
if os.path.exists(file_name):
os.remove(file_name)
f = open(file_name, 'w', encoding='utf-8')
for item in stus:
line_text = "%s\t%s\t%s" % (item['id'], item['name'], item['age'])
f.write(line_text)
f.write('\n')
f.close()
except Exception as e:
print(e)
finally:
print('file_write methon exec over..')
def main():
file_read()
while True:
print_menu()
operate = input('请输入你想要的操作:')
if operate == "1":
add_stu()
elif operate == "2":
sname = input("输入学生姓名:")
search_stu(sname)
elif operate == "3":
pass
elif operate == "4":
sname = input("请输入删除学生的姓名:")
remove_stu(sname)
elif operate == "5":
query_all_stu()
else:
file_write()
print('exit......')
break
main()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。