[Python] 学生信息管理系统 总结

  1. 需求分析:学生信息管理系统的功能
    在这里插入图片描述

  2. 模块调用:

    1. 总体展示

      def menu():  # 菜单显示
          pass
      def main():  # 主程序
          pass
      def insert():  # 插入学生信息
          pass
      def save(lst):  # 保存学生信息
          pass
      def search():  # 查找学生信息:支持id和姓名查找
          pass
      def delete():  # 删除学生信息
          pass
      def modify():  # 修改学生细信息
          pass
      def sort():  # 可选择不同学科对信息进行排序
          pass
      def total():  # 统计学生总人数
          pass
      def show():  # 展示学生信息
          pass
      def show_student(file):  # 展示学生信息核心部件
          pass
      
    2. 菜单

      def menu():  # 菜单
          print('=============================学生信息管理系统=============================')
          print('---------------------------------功能菜单--------------------------------')
          print('\t\t\t   1.录入学生信息')
          print('\t\t\t   2.查找学生信息')
          print('\t\t\t   3.删除学生信息')
          print('\t\t\t   4.修改学生信息')
          print('\t\t\t   5.排序')
          print('\t\t\t   6.统计学生总人数')
          print('\t\t\t   7.显示所有学生信息')
          print('\t\t\t   0.退出系统')
          print('\t\t\t   提示:本程序默认文件保存为.txt类型,无需输入后缀名')
      
    3. 主程序

      def main():  # 主程序
          while True:
              menu()
              choice = int(input('请输入你的选择(0-7):'))
              if choice in [0, 1, 2, 3, 4, 5, 6, 7]:
                  if choice == 0:
                      choose = input('输入q退出:')
                      if choose == 'q':
                          print('感谢使用!')
                          break
                      else:
                          continue
                  else:
                      if choice == 1:
                          insert()
                      elif choice == 2:
                          search()
                      elif choice == 3:
                          delete()
                      elif choice == 4:
                          modify()
                      elif choice == 5:
                          sort()
                      elif choice == 6:
                          total()
                      else:
                          show()
              else:
                  print('您的输入有误,请再次输入您的信息')
                  continue
      
    4. 插入

      def insert():  # 插入信息
          students_list = []
          while True:  # 循环输入
              Id = input('请输入Id:')
              if not Id:  # 防止手滑
                  print('Id有误')
                  break
              Name = input('请输入名字:')
              if not Name:
                  print('姓名有误')
                  break
      
              try:
                  English = int(input('请输入英语成绩:'))
                  Python = int(input('请输入Python成绩:'))
                  Java = int(input('请输入Java成绩:'))
              except:
                  print('输入有误,不是有效整数,请重新输入')
                  continue
              # 将录入的成绩存储到字典中
              student = {'Id': Id, 'Name': Name, 'English': English, 'Python': Python, 'Java': Java,
                         'Total': English + Python + Java}
              # 将录入的成绩保存到列表中
              students_list.append(student)  # 将字典保存到列表中
              answer = input('继续录入请输入Y/y:')
              if answer in ['Y', 'y']:
                  continue
              else:
                  save(students_list)
                  break
      
    5. 保存

      def save(lst):  # 保存
          fileName = input('请输入保存的文件名:') + '.txt'  # 文件名
          file = open(fileName, 'a', encoding='utf-8')  # 打开文件
          while not file:
              fileName = input('创建文件名失败,请重新输入:') + '.txt'
              file = open(fileName, 'a', encoding='utf-8')
          for item in lst:
              file.write(str(item) + '\n')  # 将内容写入文件中
          file.close()  # 关闭文件
          print('学生信息录入完毕')
      
    6. 搜索

      def search():  # 搜索
          fileName_new = input('请输入需要打开的学生信息文件名:') + '.txt'  # 文件名
          if os.path.exists(fileName_new):  # 如果可以打开文件
              with open(fileName_new, 'r', encoding='utf-8') as readfile:  # 上下文管理器
                  file = readfile.readlines()  # 读取信息
                  if not file:  # 如果为空
                      print('无学生信息')
                      return
                  else:  # 不为空
                      while True:  # 重复查找
                          show_student(file)  # 先展示下所有信息
                          Id = ''  # 默认化
                          Name = ''
                          mode = input('Id查找请输入1 姓名查找请输入2:')  # 通过选项影响先前默认化的变量
                          if mode == '1':
                              Id = input('请输入要查找学生的Id:')
                              if not Id:
                                  print('您输入的Id有误请重新输入')
                                  continue
                          elif mode == '2':
                              Name = input('请输入要查找学生的姓名:')
                              if not Name:
                                  print('您输入的姓名有误请重新输入')
                                  continue
                          else:
                              print('您的输入有误,请重新输入')
                              continue
                          if Id != '':
                              for item in file:
                                  d = dict(eval(item))
                                  if d['Id'] == Id:
                                      print(format_title.format('Id', 'Name', 'English', 'Python', 'Java', 'Total'))
                                      print(format_title.format(d['Id'], d['Name'], d['English'], d['Python'], d['Java'],
                                                                d['Total']))
                          elif Name != '':
                              for item in file:
                                  d = dict(eval(item))
                                  if d['Name'] == Name:
                                      print(format_title.format('Id', 'Name', 'English', 'Python', 'Java', 'Total'))
                                      print(format_title.format(d['Id'], d['Name'], d['English'], d['Python'], d['Java'],
                                                                d['Total']))
                          choice = input('继续查询请输入y/Y:')
                          if choice in ['y', 'Y']:
                              continue
                          else:
                              break
          else:
              print('您输入的文件不存在请重新输入')
              search()
      
    7. 删除

      def delete():  # 删除
          fileName_new = input('请输入需要打开的学生信息文件名:') + '.txt'
          if os.path.exists(fileName_new):  # 确定文件是否存在
              while True:  # 循环删除操作
                  with open(fileName_new, 'r', encoding='utf-8') as file:
                      file_old = file.readlines()  # 读取旧信息
                  if file_old:  # 如果信息不为空
                      show_student(file_old)  # 先展示下
                      flag = False  # 判断是否删除了
                      student_Id = input('请输入要删除学生的Id:')  # 存学生Id
                      if student_Id:  # 判断学生Id是否合法
                          with open(fileName_new, 'w', encoding='utf-8') as newfile:  # 打开新的同名文件
                              for item in file_old:  # 逐个读取学生信息
                                  d = dict(eval(item))  # 变成字典
                                  if d['Id'] != student_Id:  # 如果不匹配就写入
                                      newfile.write(str(d) + '\n')  # 要把字典变成字符串导入
                                  else:  # 匹配就显示然后跳过
                                      print(format_title.format(d['Id'], d['Name'], d['English'], d['Python'], d['Java'],
                                                                d['Total']))
                                      flag = True  # 标记为已经删除
                          if flag:
                              print(f'Id为{student_Id}的学生信息已经被删除')
                          else:
                              print(f'没有找到Id为{student_Id}的学生信息')
                          choose = input('是否继续删除y/n:')
                          if choose == 'y':
                              continue
                          else:
                              break
                      else:
                          print('学生Id输入非法,请重新输入')
                          continue
                  else:
                      print('无学生信息')
          else:
              print('输入的文件不存在')
      
    8. 修改

      def modify():  # 修改  和删除差不多,就直接CV了。。。
          fileName_new = input('请输入需要打开的学生信息文件名:') + '.txt'
          if os.path.exists(fileName_new):  # 确定文件是否存在
              with open(fileName_new, 'r', encoding='utf-8') as file:  # 读取旧信息
                  file_old = file.readlines()
              if file_old:
                  while True:  # 循环修改操作
                      show_student(file_old)  # 展示
                      flag = False  # 判断是否修改
                      student_Id = input('请输入要修改学生的Id:')  # 存学生Id
                      if student_Id:  # 判断学生Id是否合法
                          with open(fileName_new, 'w', encoding='utf-8') as newfile:  # 打开新的同名文件
                              for item in file_old:  # 逐个读取学生信息
                                  d = dict(eval(item))  # 变成字典
                                  if d['Id'] != student_Id:  # 如果不符合就写入
                                      newfile.write(str(d) + '\n')
                                  else:  # 符合就显示然后修改
                                      print(f'Id为{student_Id}的学生信息为{item}', end='')
                                      print('请输入修改后的数据')
                                      while True:
                                          Id = input('请输入Id:')
                                          if not Id:
                                              print('Id有误')
                                              break
                                          Name = input('请输入姓名:')
                                          if not Name:
                                              print('姓名有误')
                                          try:
                                              English = int(input('请输入英语成绩:'))
                                              Python = int(input('请输入Python成绩:'))
                                              Java = int(input('请输入Java成绩:'))
                                          except:
                                              print('您输入的成绩不是正常整数,请重新输入')
                                              continue
                                          student = {'Id': Id, 'Name': Name, 'English': English, 'Python': Python,
                                                     'Java': Java, 'Total': English + Python + Java}
                                          newfile.write(str(student) + '\n')
                                          flag = True  # 标记为已经删除
                                          break
                          if flag:
                              print(f'Id为{student_Id}的学生信息已经修改')
                          else:
                              print(f'没有找到Id为{student_Id}的学生信息')
                          choose = input('是否继续修改y/n:')
                          if choose == 'y':  # 也可以直接调用自身函数进行重复修改
                              continue
                          else:
                              break
                      else:
                          print('学生Id输入非法,请重新输入')
                          continue
              else:
                  print('读取学生信息为空')
          else:
              print('输入的文件不存在')
      
    9. 排序

      def sort():  # 排序
          cho = False # 初始化
          filename_new = input('请输入需要排序的学生信息文件名:') + '.txt'
          if os.path.exists(filename_new):  # 如果能打开
              with open(filename_new, 'r', encoding='utf-8') as readfile:  # 上下文管理器
                  file = readfile.readlines()  # 读取信息
                  if not file:
                      print('没有学生信息')
                      return
                  else:
                      student_list = []  # 存储读取到的学生信息
                      for item in file:
                          student_list.append(dict(eval(item)))  # 此时我已经转换成字典了,之后显示时就不用转换了
              while True:
                  choice = input('输入0来升序,输入1来降序:')
                  if choice in ['0', '1']:  # 决定升降序
                      if choice == '0':
                          choice = False
                      else:
                          choice = True
                      while True:
                          choose = input('输入0按照Id排序,输入1按照English排序,输入2按照Python排序,输入3按照Java排序,输入4按照Total排序:')
                          if choose in ['0', '1', '2', '3', '4']:
                              if choose == '0':
                                  student_list.sort(key=lambda x: int(x['Id']), reverse=choice)  # 排序
                              elif choose == '1':
                                  student_list.sort(key=lambda x: int(x['English']), reverse=choice)
                              elif choose == '2':
                                  student_list.sort(key=lambda x: int(x['Python']), reverse=choice)
                              elif choose == '3':
                                  student_list.sort(key=lambda x: int(x['Java']), reverse=choice)
                              else:
                                  student_list.sort(key=lambda x: int(x['Total']), reverse=choice)
                              show_student(student_list)  # 展示排序结果
                              cho = input('继续排序请输入y/Y:')
                              break
                          else:
                              print('您的输入有误,请重新输入')
                              continue
                      if cho in ['y', 'Y']:
                          continue
                      break
                  else:
                      print('你的输入有误,请重新输入')
                      continue
          else:
              print('您输入的文件不存在')
      
    10. 统计学生人数、

      def total():  # 统计人数
          filename_new = input('请输入需要统计的学生信息文件名:') + '.txt'
          if os.path.exists(filename_new):
              with open(filename_new, 'r', encoding='utf-8') as readfile:
                  print('一共有{0}名学生'.format(len(readfile.readlines())))
          else:
              print('该文件不存在,请重新输入')
              total()
      
    11. 展示

      def show():  # 展示信息
          filename_new = input('请输入需要展示的文件名:') + '.txt'
          if os.path.exists(filename_new):
              with open(filename_new, 'r', encoding='utf-8') as readfile:
                  file = readfile.readlines()
                  show_student(file)
          else:
              print('输入的文件不存在')
      
    12. 展示核心

      def show_student(file):  # 展示信息的核心
          if not file:
              print('无学生信息')
          else:
              print(format_title.format('Id', 'Name', 'English', 'Python', 'Java', 'Total'))
              for item in file:
                  if not isinstance(item, dict):
                      d = dict(eval(item))
                  else:
                      d = item
                  print(format_title.format(d['Id'], d['Name'], d['English'], d['Python'], d['Java'], d['Total']))
      
  3. 总代码:

    #!/usr/bin/env Python 
    # -*- coding:utf-8 -*-
    import os
    
    
    def menu():  # 菜单
        print('=============================学生信息管理系统=============================')
        print('---------------------------------功能菜单--------------------------------')
        print('\t\t\t   1.录入学生信息')
        print('\t\t\t   2.查找学生信息')
        print('\t\t\t   3.删除学生信息')
        print('\t\t\t   4.修改学生信息')
        print('\t\t\t   5.排序')
        print('\t\t\t   6.统计学生总人数')
        print('\t\t\t   7.显示所有学生信息')
        print('\t\t\t   0.退出系统')
        print('\t\t\t   提示:本程序默认文件保存为.txt类型,无需输入后缀名')
    
    
    def main():  # 主程序
        while True:
            menu()
            choice = int(input('请输入你的选择(0-7):'))
            if choice in [0, 1, 2, 3, 4, 5, 6, 7]:
                if choice == 0:
                    choose = input('输入q退出:')
                    if choose == 'q':
                        print('感谢使用!')
                        break
                    else:
                        continue
                else:
                    if choice == 1:
                        insert()
                    elif choice == 2:
                        search()
                    elif choice == 3:
                        delete()
                    elif choice == 4:
                        modify()
                    elif choice == 5:
                        sort()
                    elif choice == 6:
                        total()
                    else:
                        show()
            else:
                print('您的输入有误,请再次输入您的信息')
                continue
    
    
    def insert():  # 插入信息
        students_list = []
        while True:  # 循环输入
            Id = input('请输入Id:')
            if not Id:  # 防止手滑
                print('Id有误')
                break
            Name = input('请输入名字:')
            if not Name:
                print('姓名有误')
                break
    
            try:
                English = int(input('请输入英语成绩:'))
                Python = int(input('请输入Python成绩:'))
                Java = int(input('请输入Java成绩:'))
            except:
                print('输入有误,不是有效整数,请重新输入')
                continue
            # 将录入的成绩存储到字典中
            student = {'Id': Id, 'Name': Name, 'English': English, 'Python': Python, 'Java': Java,
                       'Total': English + Python + Java}
            # 将录入的成绩保存到列表中
            students_list.append(student)  # 将字典保存到列表中
            answer = input('继续录入请输入Y/y:')
            if answer in ['Y', 'y']:
                continue
            else:
                save(students_list)
                break
    
    
    def save(lst):  # 保存
        fileName = input('请输入保存的文件名:') + '.txt'  # 文件名
        file = open(fileName, 'a', encoding='utf-8')  # 打开文件
        while not file:
            fileName = input('创建文件名失败,请重新输入:') + '.txt'
            file = open(fileName, 'a', encoding='utf-8')
        for item in lst:
            file.write(str(item) + '\n')  # 将内容写入文件中
        file.close()  # 关闭文件
        print('学生信息录入完毕')
    
    
    def search():  # 搜索
        fileName_new = input('请输入需要打开的学生信息文件名:') + '.txt'  # 文件名
        if os.path.exists(fileName_new):  # 如果可以打开文件
            with open(fileName_new, 'r', encoding='utf-8') as readfile:  # 上下文管理器
                file = readfile.readlines()  # 读取信息
                if not file:  # 如果为空
                    print('无学生信息')
                    return
                else:  # 不为空
                    while True:  # 重复查找
                        show_student(file)  # 先展示下所有信息
                        Id = ''  # 默认化
                        Name = ''
                        mode = input('Id查找请输入1 姓名查找请输入2:')  # 通过选项影响先前默认化的变量
                        if mode == '1':
                            Id = input('请输入要查找学生的Id:')
                            if not Id:
                                print('您输入的Id有误请重新输入')
                                continue
                        elif mode == '2':
                            Name = input('请输入要查找学生的姓名:')
                            if not Name:
                                print('您输入的姓名有误请重新输入')
                                continue
                        else:
                            print('您的输入有误,请重新输入')
                            continue
                        if Id != '':
                            for item in file:
                                d = dict(eval(item))
                                if d['Id'] == Id:
                                    print(format_title.format('Id', 'Name', 'English', 'Python', 'Java', 'Total'))
                                    print(format_title.format(d['Id'], d['Name'], d['English'], d['Python'], d['Java'],
                                                              d['Total']))
                        elif Name != '':
                            for item in file:
                                d = dict(eval(item))
                                if d['Name'] == Name:
                                    print(format_title.format('Id', 'Name', 'English', 'Python', 'Java', 'Total'))
                                    print(format_title.format(d['Id'], d['Name'], d['English'], d['Python'], d['Java'],
                                                              d['Total']))
                        choice = input('继续查询请输入y/Y:')
                        if choice in ['y', 'Y']:
                            continue
                        else:
                            break
        else:
            print('您输入的文件不存在请重新输入')
            search()
    
    
    def delete():  # 删除
        fileName_new = input('请输入需要打开的学生信息文件名:') + '.txt'
        if os.path.exists(fileName_new):  # 确定文件是否存在
            while True:  # 循环删除操作
                with open(fileName_new, 'r', encoding='utf-8') as file:
                    file_old = file.readlines()  # 读取旧信息
                if file_old:  # 如果信息不为空
                    show_student(file_old)  # 先展示下
                    flag = False  # 判断是否删除了
                    student_Id = input('请输入要删除学生的Id:')  # 存学生Id
                    if student_Id:  # 判断学生Id是否合法
                        with open(fileName_new, 'w', encoding='utf-8') as newfile:  # 打开新的同名文件
                            for item in file_old:  # 逐个读取学生信息
                                d = dict(eval(item))  # 变成字典
                                if d['Id'] != student_Id:  # 如果不匹配就写入
                                    newfile.write(str(d) + '\n')  # 要把字典变成字符串导入
                                else:  # 匹配就显示然后跳过
                                    print(format_title.format(d['Id'], d['Name'], d['English'], d['Python'], d['Java'],
                                                              d['Total']))
                                    flag = True  # 标记为已经删除
                        if flag:
                            print(f'Id为{student_Id}的学生信息已经被删除')
                        else:
                            print(f'没有找到Id为{student_Id}的学生信息')
                        choose = input('是否继续删除y/n:')
                        if choose == 'y':
                            continue
                        else:
                            break
                    else:
                        print('学生Id输入非法,请重新输入')
                        continue
                else:
                    print('无学生信息')
        else:
            print('输入的文件不存在')
    
    
    def modify():  # 修改  和删除差不多,就直接CV了。。。
        fileName_new = input('请输入需要打开的学生信息文件名:') + '.txt'
        if os.path.exists(fileName_new):  # 确定文件是否存在
            with open(fileName_new, 'r', encoding='utf-8') as file:  # 读取旧信息
                file_old = file.readlines()
            if file_old:
                while True:  # 循环修改操作
                    show_student(file_old)  # 展示
                    flag = False  # 判断是否修改
                    student_Id = input('请输入要修改学生的Id:')  # 存学生Id
                    if student_Id:  # 判断学生Id是否合法
                        with open(fileName_new, 'w', encoding='utf-8') as newfile:  # 打开新的同名文件
                            for item in file_old:  # 逐个读取学生信息
                                d = dict(eval(item))  # 变成字典
                                if d['Id'] != student_Id:  # 如果不符合就写入
                                    newfile.write(str(d) + '\n')
                                else:  # 符合就显示然后修改
                                    print(f'Id为{student_Id}的学生信息为{item}', end='')
                                    print('请输入修改后的数据')
                                    while True:
                                        Id = input('请输入Id:')
                                        if not Id:
                                            print('Id有误')
                                            break
                                        Name = input('请输入姓名:')
                                        if not Name:
                                            print('姓名有误')
                                        try:
                                            English = int(input('请输入英语成绩:'))
                                            Python = int(input('请输入Python成绩:'))
                                            Java = int(input('请输入Java成绩:'))
                                        except:
                                            print('您输入的成绩不是正常整数,请重新输入')
                                            continue
                                        student = {'Id': Id, 'Name': Name, 'English': English, 'Python': Python,
                                                   'Java': Java, 'Total': English + Python + Java}
                                        newfile.write(str(student) + '\n')
                                        flag = True  # 标记为已经删除
                                        break
                        if flag:
                            print(f'Id为{student_Id}的学生信息已经修改')
                        else:
                            print(f'没有找到Id为{student_Id}的学生信息')
                        choose = input('是否继续修改y/n:')
                        if choose == 'y':  # 也可以直接调用自身函数进行重复修改
                            continue
                        else:
                            break
                    else:
                        print('学生Id输入非法,请重新输入')
                        continue
            else:
                print('读取学生信息为空')
        else:
            print('输入的文件不存在')
    
    
    def sort():  # 排序
        cho = False # 初始化
        filename_new = input('请输入需要排序的学生信息文件名:') + '.txt'
        if os.path.exists(filename_new):  # 如果能打开
            with open(filename_new, 'r', encoding='utf-8') as readfile:  # 上下文管理器
                file = readfile.readlines()  # 读取信息
                if not file:
                    print('没有学生信息')
                    return
                else:
                    student_list = []  # 存储读取到的学生信息
                    for item in file:
                        student_list.append(dict(eval(item)))  # 此时我已经转换成字典了,之后显示时就不用转换了
            while True:
                choice = input('输入0来升序,输入1来降序:')
                if choice in ['0', '1']:  # 决定升降序
                    if choice == '0':
                        choice = False
                    else:
                        choice = True
                    while True:
                        choose = input('输入0按照Id排序,输入1按照English排序,输入2按照Python排序,输入3按照Java排序,输入4按照Total排序:')
                        if choose in ['0', '1', '2', '3', '4']:
                            if choose == '0':
                                student_list.sort(key=lambda x: int(x['Id']), reverse=choice)  # 排序
                            elif choose == '1':
                                student_list.sort(key=lambda x: int(x['English']), reverse=choice)
                            elif choose == '2':
                                student_list.sort(key=lambda x: int(x['Python']), reverse=choice)
                            elif choose == '3':
                                student_list.sort(key=lambda x: int(x['Java']), reverse=choice)
                            else:
                                student_list.sort(key=lambda x: int(x['Total']), reverse=choice)
                            show_student(student_list)  # 展示排序结果
                            cho = input('继续排序请输入y/Y:')
                            break
                        else:
                            print('您的输入有误,请重新输入')
                            continue
                    if cho in ['y', 'Y']:
                        continue
                    break
                else:
                    print('你的输入有误,请重新输入')
                    continue
        else:
            print('您输入的文件不存在')
    
    
    def total():  # 统计人数
        filename_new = input('请输入需要统计的学生信息文件名:') + '.txt'
        if os.path.exists(filename_new):
            with open(filename_new, 'r', encoding='utf-8') as readfile:
                print('一共有{0}名学生'.format(len(readfile.readlines())))
        else:
            print('该文件不存在,请重新输入')
            total()
    
    
    def show():  # 展示信息
        filename_new = input('请输入需要展示的文件名:') + '.txt'
        if os.path.exists(filename_new):
            with open(filename_new, 'r', encoding='utf-8') as readfile:
                file = readfile.readlines()
                show_student(file)
        else:
            print('输入的文件不存在')
    
    
    def show_student(file):  # 展示信息的核心
        if not file:
            print('无学生信息')
        else:
            print(format_title.format('Id', 'Name', 'English', 'Python', 'Java', 'Total'))
            for item in file:
                if not isinstance(item, dict):
                    d = dict(eval(item))
                else:
                    d = item
                print(format_title.format(d['Id'], d['Name'], d['English'], d['Python'], d['Java'], d['Total']))
    
    
    format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'  # 排版
    main()
    
  4. 效果展示:
    发不了视频。。。

  5. 总结:

    这个是我跟着B站上的教程做的,做的过程中也修改了跟多地方,做这个的目的也是为了练练手,学习下python,通过这次的体验,我也对Python的文件IO操作有了更深的认识,对Python的排序也更加了解了,同时我也更加清楚地认识了os.path这个模块地具体用法

  6. 学习连接
    https://www.bilibili.com/video/BV1wD4y1o7AS?p=1
    编程软件:Pycharm

Logo

快速构建 Web 应用程序

更多推荐