Python学⽣信息管理系统(增删改查)
def meun():
menu_info = '''+************************+
|1)添加学⽣信息|
|2)显⽰所有学⽣的信息|
|3)删除学⽣信息|
|4)修改学⽣信息|
| 5)保存学⽣信息到⽂件()    |
| 6)从⽂件中读取数据()        |
|退出:其他任意按键<回车>|
+*******************************************+
'''
print(menu_info)
#|1)添加学⽣信息
def add_student_info():
l=[]
while True:
name=input('请输⼊学⽣名字: ')
if not name:
break
try:
age=int(input('学⽣年龄: '))
sex=input('性别: ')
tel=int(input(':'))
except:
print('输⼊错误,请重新输⼊')
continue
lst={'name':name,'age':age,'sex':sex,'tel':tel}
l.append(lst)
return l
# |2)显⽰所有学⽣的信息
def show_student_info(student_info,del_name=''):
if not student_info:
pritn('⽆学⽣信息,返回菜单')
return
# center()返回⼀个原字符串居中, 并使⽤空格填充⾄长度width的新字符串。默认填充字符为空格
print('姓名'.center(10), '年龄'.center(4), '性别'.center(4),'联系⽅式'.center(12))
for i in student_info:
('name').center(10),('age')).center(4),('sex')).center(4),('tel')).center(12))
# |3)删除学⽣信息
def del_student_info(student_info,del_name=''):
if not del_name:
del_name = input("请输⼊删除的学⽣姓名:")
for info in student_info:
if del_name == ("name"):
return info
raise IndexError("学⽣信息不匹配,没有到%s" %del_name)
# |4)修改学⽣信息
def mod_student_info(student_info):
mod_name=input('请输⼊要修改的学⽣名称:')
for info in student_info:
('name')==mod_name:
mod_age=int(input('请输⼊学⽣年龄:'))
mod_sex=input('请输⼊学⽣性别:')
mod_tel=int(input('请输⼊学⽣:'))
info={'name':mod_name,'age':mod_age,'sex':mod_sex,'tel':mod_tel,}
return info
raise IndexError('姓名不匹配:%s'%mod_name)
#⽤于年龄排序:
def get_age(*l):
def get_age(*l):
for x in l:
('age')
from operator import itemgetter
# |5)保存学⽣信息到⽂件()
def save_info(student_info):
try:
# 以写模式打开,并清空⽂件内容
student_txt = open('','w')
except Exception as e:
# ⽂件不存在,创建⽂件并打开
student_txt = open('','x')
for info in student_info:
student_txt.write(str(info)+'\n')
student_txt,close()
# |6)从⽂件中读取数据
def read_info():
old_info = []
try:
students_txt = open("")
except:
print("暂未保存数据信息")  # 打开失败,⽂件不存在说明没有数据保存        return
while True:
info = adline()
if not info:
break
# print(info)
info = info.rstrip()
# print(info)
info = info[1:-1]
# print(info)
student_dict = {}
for x in info.split(","):
# print(x)
key_value = []
for k in x.split(":"):
k = k.strip()
# print(k)
if k[0] == k[-1] and len(k) > 2:
key_value.append(k[1:-1])
else:
key_value.append(int(k))
# print(key_value)
student_dict[key_value[0]] = key_value[1]  # 学⽣信息添加
# print(student_dict)
old_info.append(student_dict)  # 所有学⽣信息汇总
students_txt.close()
return old_info
def main():
student_info=[]
while True:
meun()
number=input('请输⼊您的选项: ')
if number == '1':
student_info+=add_student_info()
elif number == '2':
show_student_info(student_info)
python怎么读取txtelif number == '3':
try:
try:
ve(del_student_info(student_info))
except Exception as e:
print(e)
elif number == '4':
try:
student=mod_student_info(student_info)
except Exception as e:
print(e)
else:
# 根据输⼊的名字,从列表删除该信息,再重新添加该学⽣最新信息
ve(del_student_info(student_info, del_("name")))                student_info.append(student)
elif number == '5':
save_info(student_info)
elif number == '6':
student_info = read_info()
else:
break
input('--回车返回菜单--')
main()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。