
自学Python,编写了一个学生信息统计的小Demo。
import os
filename = 'student.txt'
def menum():
print("==========================学生管理信息=============")
print("------------------------------功能菜单--------------")
print("\t\t\t\t\t\t\t1.录入学生信息")
print("\t\t\t\t\t\t\t2.查找学生信息")
print("\t\t\t\t\t\t\t3.删除学生信息")
print("\t\t\t\t\t\t\t4.修改学生信息")
print("\t\t\t\t\t\t\t5.排序")
print("\t\t\t\t\t\t\t6.统计学生总人数")
print("\t\t\t\t\t\t\t7.显示学生信息")
print("\t\t\t\t\t\t\t0.退出系统")
print("--------------------------------------------------")
def main():
while True:
menum()
choice = int(input('请选择'))
if choice in range(0,8):
if choice == 0:
answer = input("您确定要退出系统吗?y/n")
if answer == 'y' or answer == 'Y':
print('谢谢您的使用!!!')
break
else:
continue
elif choice == 1:
insert()
elif choice == 2:
search()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
sort()
elif choice == 6:
total()
elif choice == 7:
show()
pass
def insert():
student_list = []
while True:
id = input('请输入ID(如1001):')
if not id:
break
name = input('请输入学生姓名:')
if not name:
break
try:
english = float(input('请输入english成绩:'))
python = float(input('请输入python成绩:'))
java = float(input('请输入java成绩:'))
except:
print('输入成绩无效,请重新输入')
student_info = {'id':id,'name':name,'english':english,'python':python,'java':java}
student_list.append(student_info)
answer = input("是否继续添加?y/n\n")
if answer == 'y' or answer == 'Y':
continue
else:
break
save(student_list)
print('录入完毕')
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():
student_query = [];
while True:
id = ''
name = ''
if os.path.exists(filename):
mode = input('ID查找请输入1,Name查找请输入2')
if int(mode) == 1:
id = input('请输入学生ID')
elif int(mode) == 2:
name = input('请输入学生Name')
else:
print('不支持此类型查询')
search()
with open(filename,'r',encoding='utf-8') as rFile:
student = rFile.readlines()
for item in student:
d = dict(eval(item))
if d!='':
if d['id'] == id:
student_query.append(d)
elif name!='':
if d['name'] == name:
student_query.append(d)
show_student(student_query)
student_query.clear()
answer = input('是否继续查询?y/n\n')
if answer=='y' or answer=='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{:^10}\t{:^10}\t{:^8}\t'
print(format_title.format('ID','姓名','英语成绩','Python成绩','Java成绩','总成绩'))
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}\t'
for item in lst:
print(format_data.format(item.get('id'),
item.get('name'),
item.get('english'),
item.get('python'),
item.get('java'),
float(item.get('english'))+float(item.get('python'))+float(item.get('java'))
))
def delete():
while True:
student_id = input('请输入学生ID:')
if student_id != '':
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as file:
student_old = file.readlines()
else:
student_old = []
flag = False
if student_old:
with open(filename,'w',encoding='utf-8') as wfile:
d = {}
for item in student_old:
d = dict(eval(item))
if d['id'] != student_id:
wfile.write(str(d)+'\n')
else:
flag = True
if flag:
print(f'id为{student_id}的学生信息已被删除')
else:
print(f'id为{student_id}的学生信息不存在')
else:
print('无学生信息')
break
show()
answer = input('是否继续删除?y/n\n')
if answer=='y' or answer=='Y':
continue
else:
break
def modify():
while True:
show()
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
student_old = rfile.readlines()
else:
return
student_id = input('请输入学生ID')
with open(filename,'w',encoding='utf-8') as wfile:
for item in student_old:
d = dict(eval(item))
if d['id'] == student_id:
print('找到学生信息,可以进行修改操作了')
while True:
try:
d['name'] = input('请输入学生姓名')
d['english'] = input('请输入english成绩')
d['python'] = input('请输入python成绩')
d['java'] = input('请输入java成绩')
except:
print("输入信息有误")
else:
break
wfile.write(str(d)+'\n')
print('修改成功')
else:
wfile.write(str(d)+'\t')
answer = input('是否继续修改其他学生信息?y/n\n')
if answer=='y' or answer=='Y':
modify()
else:
break
def sort():
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
student_new = []
student_list = rfile.readlines()
for item in student_list:
d = dict(eval(item))
student_new.append(d)
else:
return
asc_or_desc = input('请选择排序方式0asc1desc')
if asc_or_desc == '0':
asc_or_desc_bool = False
elif asc_or_desc == '1':
asc_or_desc_bool = True
else:
print('输入有误')
sort()
mode = input('选择详细排序方式1English2Python3Java0Total')
if mode == '1':
student_new.sort(key=lambda self:float(self['english']),reverse=asc_or_desc_bool)
elif mode == '2':
student_new.sort(key=lambda self: float(self['python']), reverse=asc_or_desc_bool)
elif mode == '3':
student_new.sort(key=lambda self: float(self['java']), reverse=asc_or_desc_bool)
elif mode == '4':
student_new.sort(key=lambda self: (float(self['java'])+float(self['python'])+float(self['english'])), reverse=asc_or_desc_bool)
else:
print('详细排序输入有误')
sort()
show_student(student_new)
def total():
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
students = rfile.readlines()
if students:
print('一共有{}名学生'.format(len(students)))
else:
print('没有学生信息')
else:
print('无数据')
def show():
student_info = []
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
students = rfile.readlines()
for item in students:
student_info.append(eval(item))
if students:
show_student(student_info)
else:
print('信息为空')
else:
print('文件不存在')
if __name__ == '__main__':
main()