随着信息技术的不断发展,许多工作机器取代了人工。通过Python对学生信息管理系统的编写,在很大程度上减轻了老师们的负担,使得学校的办事效率有了更大的提高。
Python 语言是在 ABC 教学语言的基础上发展来的。Python 语言最大的特点就是简单。然而对于 Python,网络上流传着“人生苦短,我用 Python”的说法。
主要函数如下(示例):
def main() #主函数 def menm() #菜单函数 def insert() #插入学生信息函数 def save() #保存学生信息函数 def search() #查询学生信息函数 (这里包含按学号查找和按姓名查找) def show_student() #显示全部学生信息 def delete() #删除学生信息 def modify() #修改学生信息 def sort() #将学生信息按成绩排序成绩(可以选择降序还是升序排列。也可以选择按照 某一门成绩进行排序)
代码如下(示例):
def main(): while True: menm() choice = int(input(请选择)) if choice in [0, 1, 2, 3, 4, 5, 6, 7]: 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()
def menm(): print(=========================学生信息管理系统==========================) print(------------------------功能菜单----------------------------------) print( 1.录入学生信息) print( 2.查找学生信息) print( 3.删除学生信息) print( 4.修改学生信息) print( 5.排序) print( 6.统计学生总数) print( 7.显示所有学生信息) print( 0.退出系统) print(-----------------------------------------------------------------)
def insert(): student_list = [] while True: id = input(请输入ID(如1001):) if not id: break name = input(请输入姓名:) if not name: break try: python = int(input(请输入Python成绩:)) java = int(input(请输入java成绩:)) except: print(输入无效,不是整数类型,请重新输入) continue # 将录入的学生信息保存到字典中 student = {id: id, name: name, python: python, java: java} # 将学生信息天加到列表当中 student_list.append(student) answer = input(是否继续添加?y/n ) if answer == y: continue else: break # 调用save()函数 save(student_list) print(学生信息录入完毕!!!)
def save(lst): try: stu_text = open(filename, a, encoding=utf-8) except: stu_text = open(filename, w, encoding=utf-8) for item in lst: stu_text.write(str(item) + ) stu_text.close()
def search(): student_query = [] while True: id = name = if os.path.exists(filename): mode = input(按ID查找请输入1,按姓名查找请输入2:) if mode == 1: id = input(请输入要查找的学生ID:) elif mode == 2: name = input(请输入要查找的学生姓名:) else: print(您的输入有误,请重新输入) continue with open(filename, r, encoding=utf-8) as rfile: student = rfile.readlines() for item in student: d = dict(eval(item)) if id != : 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) if answer == y: continue else: break else: print(暂未保存学生信息) return
def show_student(lst): if len(lst) == 0: print(没有查询到学生信息,无数据!!!) return # 定义显示格式 format_title = {:^6} {:^12} {:^8} {:^10} {:^8} print(format_title.format(ID, 姓名, Python成绩, java成绩, 总成绩)) # 定义内容显示格式 format_data = {:^6} {:^12} {:^8} {:^10} {:^8} for item in lst: print(format_data.format(item[id], item[name], item[python], item[java], int(item.get(python)) + int(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) + ) else: flag = True if flag: print(fid为{student_id}的学生信息已被删除) else: print(f没有找到ID为{student_id}的学生信息) else: print(无学生信息) break show() # 删除后重新显示学生信息 answer = input(是否继续删除呢?y/n + ) if answer == y: continue else: break
def modify(): 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[python] = input(请输入Python成绩:) d[java] = input(请输入java成绩:) except: print(您的输入有误,请重新输入!!!) else: break wfile.write(str(d) + ) print(修改成功) else: wfile.write(str(d) + ) answer = input(您是否继续修改呢?y/n) if answer == y: modify()
def sort(): show() if os.path.exists(filename): with open(filename,r,encoding=utf-8) as rfile: student_lst=rfile.readlines() student_new=[] for item in student_lst: d=dict(eval(item)) student_new.append(d) else: return choice=input(请选择排序方式(0-升序,1-降序)) if choice==0: choice_bool=False elif choice==1: choice_bool=True else: print(您的输入有误,请重新输入!!!!) sort() mode =input(请选择排序方式(1-按Python成绩排序,2-按java成绩排序,3-按总成绩排序)) if mode==1: student_new.sort(key=lambda x:int(x[python]),reverse=choice_bool) elif mode==2: student_new.sort(key=lambda x:int(x[java]),reverse=choice_bool) elif mode==3: student_new.sort(key=lambda x:int(x[python])+int(x[java]),reverse=choice_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(f一共有{len(students)}名学生) else: print(还没有录入学生信息!!!) else: print(暂未保存数据信息!!!)
import os #引入os模块 filename = student.txt def main(): while True: menm() choice = int(input(请选择)) if choice in [0, 1, 2, 3, 4, 5, 6, 7]: 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() def menm(): print(=========================学生信息管理系统==========================) print(------------------------功能菜单----------------------------------) print( 1.录入学生信息) print( 2.查找学生信息) print( 3.删除学生信息) print( 4.修改学生信息) print( 5.排序) print( 6.统计学生总数) print( 7.显示所有学生信息) print( 0.退出系统) print(-----------------------------------------------------------------) def insert(): student_list = [] while True: id = input(请输入ID(如1001):) if not id: break name = input(请输入姓名:) if not name: break try: python = int(input(请输入Python成绩:)) java = int(input(请输入java成绩:)) except: print(输入无效,不是整数类型,请重新输入) continue # 将录入的学生信息保存到字典中 student = {id: id, name: name, python: python, java: java} # 将学生信息天加到列表当中 student_list.append(student) answer = input(是否继续添加?y/n ) if answer == y: continue else: break # 调用save()函数 save(student_list) print(学生信息录入完毕!!!) def save(lst): try: stu_text = open(filename, a, encoding=utf-8) except: stu_text = open(filename, w, encoding=utf-8) for item in lst: stu_text.write(str(item) + ) stu_text.close() def search(): student_query = [] while True: id = name = if os.path.exists(filename): mode = input(按ID查找请输入1,按姓名查找请输入2:) if mode == 1: id = input(请输入要查找的学生ID:) elif mode == 2: name = input(请输入要查找的学生姓名:) else: print(您的输入有误,请重新输入) continue with open(filename, r, encoding=utf-8) as rfile: student = rfile.readlines() for item in student: d = dict(eval(item)) if id != : 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) if answer == y: continue else: break else: print(暂未保存学生信息) return def show_student(lst): if len(lst) == 0: print(没有查询到学生信息,无数据!!!) return # 定义显示格式 format_title = {:^6} {:^12} {:^8} {:^10} {:^8} print(format_title.format(ID, 姓名, Python成绩, java成绩, 总成绩)) # 定义内容显示格式 format_data = {:^6} {:^12} {:^8} {:^10} {:^8} for item in lst: print(format_data.format(item[id], item[name], item[python], item[java], int(item.get(python)) + int(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) + ) else: flag = True if flag: print(fid为{student_id}的学生信息已被删除) else: print(f没有找到ID为{student_id}的学生信息) else: print(无学生信息) break show() # 删除后重新显示学生信息 answer = input(是否继续删除呢?y/n + ) if answer == y: continue else: break def modify(): 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[python] = input(请输入Python成绩:) d[java] = input(请输入java成绩:) except: print(您的输入有误,请重新输入!!!) else: break wfile.write(str(d) + ) print(修改成功) else: wfile.write(str(d) + ) answer = input(您是否继续修改呢?y/n) if answer == y: modify() def sort(): show() if os.path.exists(filename): with open(filename,r,encoding=utf-8) as rfile: student_lst=rfile.readlines() student_new=[] for item in student_lst: d=dict(eval(item)) student_new.append(d) else: return choice=input(请选择排序方式(0-升序,1-降序)) if choice==0: choice_bool=False elif choice==1: choice_bool=True else: print(您的输入有误,请重新输入!!!!) sort() mode =input(请选择排序方式(1-按Python成绩排序,2-按java成绩排序,3-按总成绩排序)) if mode==1: student_new.sort(key=lambda x:int(x[python]),reverse=choice_bool) elif mode==2: student_new.sort(key=lambda x:int(x[java]),reverse=choice_bool) elif mode==3: student_new.sort(key=lambda x:int(x[python])+int(x[java]),reverse=choice_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(f一共有{len(students)}名学生) else: print(还没有录入学生信息!!!) else: print(暂未保存数据信息!!!) def show(): student_lst=[] if os.path.exists(filename): with open(filename,r,encoding=utf-8) as rfile: students=rfile.readlines() for item in students: student_lst.append(eval(item)) if student_lst: show_student(student_lst) else: print(暂未保存数据!!!) if __name__ == __main__: main()
这里对文章进行总结:通过上述代码就可以用Python写一个简洁的学生信息管理系统,这里并没有引入GUI视图。注意引入os模块,否则程序会报错。