前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python学生管理系统示例

Python学生管理系统示例

作者头像
py3study
发布2020-01-09 11:26:26
7490
发布2020-01-09 11:26:26
举报
文章被收录于专栏:python3python3

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.')

代码语言:javascript
复制
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()

代码语言:javascript
复制
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()

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档