利用Python实现学生信息管理系统的完整实例


Posted in Python onDecember 30, 2020

项目要求:

利用Python实现学生信息管理系统的完整实例

读完题目,首先我们要确定程序思路

我们要全部通过类去实现

也就是 我们要实现管理员、学生、讲师、课程、教师五个类

管理员类

class Administration(object):
 def __init__(self):
  self.data = self.__load()
  self.login_data = {}

 def __load(self) -> list:
  try:
   Adm = open('Administration.csv', 'r')
   readers = csv.DictReader(Adm)
   Adm_dict = [dict(i) for i in readers]
   Adm.close()
   return Adm_dict
  except Exception:
   print('文件读取失败')

 def see_course(self):
  print('当前所有的课程信息为:')
  for i in cou.data:
   print(i)
  print()

 def see_student(self):
  print('当前所有的学生信息为:')
  for i in stu.data:
   print(i)
  print()

 def see_student_course(self):
  print('当前所有学生的选课信息为:')
  for i in stu.data:
   print(i['姓名'], '的选课信息为:', i['课程'])
  print()

 def set_course(self):
  name = input('请输入你想添加的课程名')
  c = [i['名称'] for i in cou.data]
  if name in c:
   print('你准备添加的课程 已经存在')
  else:
   cou.data.append({'名称': name, '人数': 0})
   print('添加成功!')
   print()

 def set_student(self):
  name = input('请输入你想添加的学生的姓名')
  ID = input('请输入此学生的ID')
  if ID in [i['ID'] for i in stu.data]:
   print('这个ID已经存在,添加失败')
   return
  print('学生账号默认密码123456')
  password = 123456
  stu.data.append({'姓名': name, 'ID': ID, '密码': password, '课程': ''})
  print('添加成功!')
  print()

 def set_teacher(self):
  name = input('请输入你想添加的老师的姓名')
  ID = input('请输入此老师的ID')
  if ID in [i['ID'] for i in tea.data]:
   print('这个ID已经存在,添加失败')
   return
  print('老师账号默认密码123456')
  password = 123456
  tea.data.append({'姓名': name, 'ID': ID, '密码': password, '课程': ''})
  print('添加成功!')
  print()

 def app_teacher_to_class(self):
  t_name = input('请输入你想操作的老师名')
  if t_name not in [i['姓名'] for i in tea.data]:
   print('你想操作的老师不存在,请重新操作')
   return
  c_name = input('请输入你想让该老师任课的班级')
  if c_name not in [i['班名'] for i in cla.data]:
   print('你想让老师任教的班级不存在,请重新操作')
   return
  for i in tea.data:
   if i['姓名'] == t_name:
    i['班级'] = c_name
  for i in cla.data:
   if i['班名'] == c_name:
    i['任课老师'] = t_name
  print('操作成功')

 def set_class(self):
  name = input('请输入你想创建的班级的名称:')
  if name in [i['班名'] for i in cla.data]:
   print('你想创建的班级已经存在,请重新操作')
   return
  if int(input('是否添加任课老师,是输入1,否输入0')):
   t_name = input('请输入你想添加的老师名')
   if t_name not in [i['姓名'] for i in tea.data]:
    print('你想操作的老师不存在,请重新操作')
    return
  else:
   t_name = ''
  s_name_list = []
  while int(input('是否添加学生,是输入1,否输入0')):
   s_name = input('输入你想添加的学生名')
   if s_name not in [i['姓名'] for i in stu.data]:
    print('你想操作的学生不存在,请重新操作')
    return
   else:
    s_name_list.append(s_name)
  s_name = ''
  if s_name_list == []:
   cla.data.append({'班名': name, '任课老师': t_name, '学生': s_name})
   print('操作成功')
  else:
   cla.data.append({'班名': name, '任课老师': t_name,
        '学生': s_name.join(s_name_list)})
   print('操作成功')

 def app_student_to_class(self):
  s_name = input('请输入你想操作的学生名')
  if s_name not in [i['姓名'] for i in stu.data]:
   print('你想操作的学生不存在,请重新操作')
   return
  c_name = input('请输入你想让该学生进入的班级')
  if c_name not in [i['班名'] for i in cla.data]:
   print('你想让学生进入的班级不存在,请重新操作')
   return
  for i in stu.data:
   if i['姓名'] == s_name:
    i['班级'] = c_name
  for i in cla.data:
   if i['班名'] == c_name:
    i['任课老师'] = s_name
  print('操作成功')

 def save(self):
  try:
   j = open('Administration.csv', 'w')
   writer = csv.writer(j)
   writer.writerow(list(self.data[0].keys()))
   data = [list(i.values()) for i in self.data]
   writer.writerows(data)
   j.close()
  except Exception:
   print('文件读取失败')

学生类

class Student(object):
 def __init__(self):
  self.data = self.__load()
  self.login_data = {}

 def __load(self):
  try:
   Stu = open('Student.csv', 'r')
   readers = csv.DictReader(Stu)
   Stu_dict = [dict(i) for i in readers]
   Stu.close()
   return Stu_dict
  except Exception:
   print('文件读取失败')

 def see_course(self):
  print('当前所有的课程信息为:')
  for i in cou.data:
   print(i)
  print()

 def choice_course(self):
  # print(self.data)
  c = [i['名称'] for i in cou.data]
  c_name = input('请输入你想选择的课程名:')
  if c_name not in c:
   print('你选择的课程不在课程列表中')
   return
  else:
   self.login_data['课程']: str = self.login_data['课程'] + c_name + ' '
   for i in cou.data:
    if i['名称'] == c_name:
     i['人数'] = int(i['人数']) + 1
  print('操作成功')

 def see_own_course(self):
  if self.login_data['课程'] == '':
   print('你未选择任何课程')
  else:
   print(self.login_data['课程'])

 def save(self):
  try:
   j = open('Student.csv', 'w')
   writer = csv.writer(j)
   writer.writerow(list(self.data[0].keys()))
   data = [list(i.values()) for i in self.data]
   writer.writerows(data)
  except Exception:
   print('文件读取失败')

课程类

class Course(object):
 def __init__(self):
  self.data = self.__load()

 def __load(self):
  try:
   Cou = open('Course.csv', 'r')
   readers = csv.DictReader(Cou)
   Cou_dict = [dict(i) for i in readers]
   Cou.close()
   return Cou_dict
  except Exception:
   print('文件读取失败')

 def save(self):
  try:
   j = open('Course.csv', 'w')
   writer = csv.writer(j)
   writer.writerow(list(self.data[0].keys()))
   data = [list(i.values()) for i in self.data]
   writer.writerows(data)
  except Exception:
   print('文件读取失败')

教室类

class Classroom(object):
 def __init__(self):
  self.data = self.__load()

 def __load(self):
  try:
   Cla = open('Classroom.csv', 'r')
   readers = csv.DictReader(Cla)
   Cla_dict = [dict(i) for i in readers]
   Cla.close()
   return Cla_dict
  except Exception:
   print('文件读取失败')

 def save(self):
  try:
   j = open('Classroom.csv', 'w')
   writer = csv.writer(j)
   writer.writerow(list(self.data[0].keys()))
   data = [list(i.values()) for i in self.data]
   writer.writerows(data)
  except Exception:
   print('文件读取失败')

教师类

class Teacher(object):
 def __init__(self):
  self.data = self.__load()
  self.login_data = {}

 def __load(self):
  try:
   Tea = open('Teacher.csv', 'r')
   readers = csv.DictReader(Tea)
   Tea_dict = [dict(i) for i in readers]
   Tea.close()
   return Tea_dict
  except Exception:
   print('文件读取失败')

 def see_course(self):
  print('当前所有的课程信息为:')
  for i in cou.data:
   print(i)
  print()

 def see_class(self):
  print('所教班级为:', self.login_data['班级'])
  print()

 def see_class_student(self):
  print('所教班级中的学生为:')
  for i in cla.data:
   if i['班名'] == self.login_data['班级']:
    print(i['学生'])
  print()

 def save(self):
  try:
   j = open('Teacher.csv', 'w')
   writer = csv.writer(j)
   writer.writerow(list(self.data[0].keys()))
   data = [list(i.values()) for i in self.data]
   writer.writerows(data)
  except Exception:
   print('文件读取失败')

完整代码如下:

import csv
import time


class Administration(object):
 def __init__(self):
  self.data = self.__load()
  self.login_data = {}

 def __load(self) -> list:
  try:
   Adm = open('Administration.csv', 'r')
   readers = csv.DictReader(Adm)
   Adm_dict = [dict(i) for i in readers]
   Adm.close()
   return Adm_dict
  except Exception:
   print('文件读取失败')

 def see_course(self):
  print('当前所有的课程信息为:')
  for i in cou.data:
   print(i)
  print()

 def see_student(self):
  print('当前所有的学生信息为:')
  for i in stu.data:
   print(i)
  print()

 def see_student_course(self):
  print('当前所有学生的选课信息为:')
  for i in stu.data:
   print(i['姓名'], '的选课信息为:', i['课程'])
  print()

 def set_course(self):
  name = input('请输入你想添加的课程名')
  c = [i['名称'] for i in cou.data]
  if name in c:
   print('你准备添加的课程 已经存在')
  else:
   cou.data.append({'名称': name, '人数': 0})
   print('添加成功!')
   print()

 def set_student(self):
  name = input('请输入你想添加的学生的姓名')
  ID = input('请输入此学生的ID')
  if ID in [i['ID'] for i in stu.data]:
   print('这个ID已经存在,添加失败')
   return
  print('学生账号默认密码123456')
  password = 123456
  stu.data.append({'姓名': name, 'ID': ID, '密码': password, '课程': ''})
  print('添加成功!')
  print()

 def set_teacher(self):
  name = input('请输入你想添加的老师的姓名')
  ID = input('请输入此老师的ID')
  if ID in [i['ID'] for i in tea.data]:
   print('这个ID已经存在,添加失败')
   return
  print('老师账号默认密码123456')
  password = 123456
  tea.data.append({'姓名': name, 'ID': ID, '密码': password, '课程': ''})
  print('添加成功!')
  print()

 def app_teacher_to_class(self):
  t_name = input('请输入你想操作的老师名')
  if t_name not in [i['姓名'] for i in tea.data]:
   print('你想操作的老师不存在,请重新操作')
   return
  c_name = input('请输入你想让该老师任课的班级')
  if c_name not in [i['班名'] for i in cla.data]:
   print('你想让老师任教的班级不存在,请重新操作')
   return
  for i in tea.data:
   if i['姓名'] == t_name:
    i['班级'] = c_name
  for i in cla.data:
   if i['班名'] == c_name:
    i['任课老师'] = t_name
  print('操作成功')

 def set_class(self):
  name = input('请输入你想创建的班级的名称:')
  if name in [i['班名'] for i in cla.data]:
   print('你想创建的班级已经存在,请重新操作')
   return
  if int(input('是否添加任课老师,是输入1,否输入0')):
   t_name = input('请输入你想添加的老师名')
   if t_name not in [i['姓名'] for i in tea.data]:
    print('你想操作的老师不存在,请重新操作')
    return
  else:
   t_name = ''
  s_name_list = []
  while int(input('是否添加学生,是输入1,否输入0')):
   s_name = input('输入你想添加的学生名')
   if s_name not in [i['姓名'] for i in stu.data]:
    print('你想操作的学生不存在,请重新操作')
    return
   else:
    s_name_list.append(s_name)
  s_name = ''
  if s_name_list == []:
   cla.data.append({'班名': name, '任课老师': t_name, '学生': s_name})
   print('操作成功')
  else:
   cla.data.append({'班名': name, '任课老师': t_name,
        '学生': s_name.join(s_name_list)})
   print('操作成功')

 def app_student_to_class(self):
  s_name = input('请输入你想操作的学生名')
  if s_name not in [i['姓名'] for i in stu.data]:
   print('你想操作的学生不存在,请重新操作')
   return
  c_name = input('请输入你想让该学生进入的班级')
  if c_name not in [i['班名'] for i in cla.data]:
   print('你想让学生进入的班级不存在,请重新操作')
   return
  for i in stu.data:
   if i['姓名'] == s_name:
    i['班级'] = c_name
  for i in cla.data:
   if i['班名'] == c_name:
    i['任课老师'] = s_name
  print('操作成功')

 def save(self):
  try:
   j = open('Administration.csv', 'w')
   writer = csv.writer(j)
   writer.writerow(list(self.data[0].keys()))
   data = [list(i.values()) for i in self.data]
   writer.writerows(data)
   j.close()
  except Exception:
   print('文件读取失败')


class Student(object):
 def __init__(self):
  self.data = self.__load()
  self.login_data = {}

 def __load(self):
  try:
   Stu = open('Student.csv', 'r')
   readers = csv.DictReader(Stu)
   Stu_dict = [dict(i) for i in readers]
   Stu.close()
   return Stu_dict
  except Exception:
   print('文件读取失败')

 def see_course(self):
  print('当前所有的课程信息为:')
  for i in cou.data:
   print(i)
  print()

 def choice_course(self):
  # print(self.data)
  c = [i['名称'] for i in cou.data]
  c_name = input('请输入你想选择的课程名:')
  if c_name not in c:
   print('你选择的课程不在课程列表中')
   return
  else:
   self.login_data['课程']: str = self.login_data['课程'] + c_name + ' '
   for i in cou.data:
    if i['名称'] == c_name:
     i['人数'] = int(i['人数']) + 1
  print('操作成功')

 def see_own_course(self):
  if self.login_data['课程'] == '':
   print('你未选择任何课程')
  else:
   print(self.login_data['课程'])

 def save(self):
  try:
   j = open('Student.csv', 'w')
   writer = csv.writer(j)
   writer.writerow(list(self.data[0].keys()))
   data = [list(i.values()) for i in self.data]
   writer.writerows(data)
  except Exception:
   print('文件读取失败')


class Course(object):
 def __init__(self):
  self.data = self.__load()

 def __load(self):
  try:
   Cou = open('Course.csv', 'r')
   readers = csv.DictReader(Cou)
   Cou_dict = [dict(i) for i in readers]
   Cou.close()
   return Cou_dict
  except Exception:
   print('文件读取失败')

 def save(self):
  try:
   j = open('Course.csv', 'w')
   writer = csv.writer(j)
   writer.writerow(list(self.data[0].keys()))
   data = [list(i.values()) for i in self.data]
   writer.writerows(data)
  except Exception:
   print('文件读取失败')


class Classroom(object):
 def __init__(self):
  self.data = self.__load()

 def __load(self):
  try:
   Cla = open('Classroom.csv', 'r')
   readers = csv.DictReader(Cla)
   Cla_dict = [dict(i) for i in readers]
   Cla.close()
   return Cla_dict
  except Exception:
   print('文件读取失败')

 def save(self):
  try:
   j = open('Classroom.csv', 'w')
   writer = csv.writer(j)
   writer.writerow(list(self.data[0].keys()))
   data = [list(i.values()) for i in self.data]
   writer.writerows(data)
  except Exception:
   print('文件读取失败')


class Teacher(object):
 def __init__(self):
  self.data = self.__load()
  self.login_data = {}

 def __load(self):
  try:
   Tea = open('Teacher.csv', 'r')
   readers = csv.DictReader(Tea)
   Tea_dict = [dict(i) for i in readers]
   Tea.close()
   return Tea_dict
  except Exception:
   print('文件读取失败')

 def see_course(self):
  print('当前所有的课程信息为:')
  for i in cou.data:
   print(i)
  print()

 def see_class(self):
  print('所教班级为:', self.login_data['班级'])
  print()

 def see_class_student(self):
  print('所教班级中的学生为:')
  for i in cla.data:
   if i['班名'] == self.login_data['班级']:
    print(i['学生'])
  print()

 def save(self):
  try:
   j = open('Teacher.csv', 'w')
   writer = csv.writer(j)
   writer.writerow(list(self.data[0].keys()))
   data = [list(i.values()) for i in self.data]
   writer.writerows(data)
  except Exception:
   print('文件读取失败')


def login(ID, password) -> int:
 # 判断是否为管理员
 for i in adm.data:
  if ID == i['ID'] and password == i['密码']:
   adm.login_data = i
   return 1
 # 判断是否为学生
 for i in stu.data:
  if ID == i['ID'] and password == i['密码']:
   stu.login_data = i
   return 2
 # 判断是否为老师
 for i in tea.data:
  if ID == i['ID'] and password == i['密码']:
   tea.login_data = i
   return 3
 return 0


if __name__ == '__main__':
 print('欢迎来到久的学生选课系统')
 time.sleep(0.5)
 print('正在加载中~')
 adm = Administration()
 stu = Student()
 cou = Course()
 tea = Teacher()
 cla = Classroom()
 time.sleep(0.3)
 print('加载完毕')
 while True:
  ID = input('请输入你的账号:')
  password = input('请输入你的密码:')
  flag = login(ID, password)
  if not flag:
   print('输入的账号或密码错误')
   flag_1 = int(input('是否重新登录 重新登录输入1 取消登录输入0'))
   if flag_1:
    continue
   else:
    break
  elif flag == 1:
   print('欢迎管理员')
   break
  elif flag == 2:
   print('欢迎学生')
   break
  elif flag == 3:
   print('欢迎老师')
   break
 if flag == 1:
  while True:
   check = int(
    input(
     '1.创建课程\n2.创建学生账号\n3.查看所有课程\n4.查看所有学生\n5.查看所有学生的选课信息\n6.创建讲师'
     '\n7.为讲师指定班级\n8.创建班级\n9.为学生指定班级\n10.退出程序'))
   if check == 1:
    adm.set_course()
   elif check == 2:
    adm.set_student()
   elif check == 3:
    adm.see_course()
   elif check == 4:
    adm.see_student()
   elif check == 5:
    adm.see_student_course()
   elif check == 6:
    adm.set_teacher()
   elif check == 7:
    adm.app_teacher_to_class()
   elif check == 8:
    adm.set_class()
   elif check == 9:
    adm.app_student_to_class()
   elif check == 10:
    break
 elif flag == 2:
  while True:
   check = int(input('1.查看所有课程\n2.查看所选课程\n3.选择课程\n4.退出程序'))
   if check == 1:
    stu.see_course()
   elif check == 2:
    stu.see_own_course()
   elif check == 3:
    stu.choice_course()
   elif check == 4:
    break
 elif flag == 3:
  while True:
   check = int(input('1.查看所有课程\n2.查看所教班级\n3.查看班级中的学生\n4.退出程序'))
   if check == 1:
    tea.see_course()
   elif check == 2:
    tea.see_class()
   elif check == 3:
    tea.see_class_student()
   elif check == 4:
    break
 print('数据存储中~')
 adm.save()
 tea.save()
 stu.save()
 cla.save()
 cou.save()
 time.sleep(0.5)
 print('感谢您的使用!')

整个系统的数据全部单独存在,并存储在csv文件中

效果图:

利用Python实现学生信息管理系统的完整实例

总结

到此这篇关于利用Python实现学生信息管理系统的文章就介绍到这了,更多相关Python实现学生信息管理系统内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python格式化字符串实例总结
Sep 28 Python
在Python中使用Neo4j数据库的教程
Apr 16 Python
在Django中同时使用多个配置文件的方法
Jul 22 Python
图文详解WinPE下安装Python
May 17 Python
python实现kNN算法
Dec 20 Python
pandas去除重复列的实现方法
Jan 29 Python
Python使用dict.fromkeys()快速生成一个字典示例
Apr 24 Python
pytorch 固定部分参数训练的方法
Aug 17 Python
用python画一只可爱的皮卡丘实例
Nov 21 Python
Python数据可视化:泊松分布详解
Dec 07 Python
Pycharm 2020年最新激活码(亲测有效)
Sep 18 Python
python如何利用Mitmproxy抓包
Oct 10 Python
使用gunicorn部署django项目的问题
Dec 30 #Python
pyspark对Mysql数据库进行读写的实现
Dec 30 #Python
python实现无边框进度条的实例代码
Dec 30 #Python
python中的列表和元组区别分析
Dec 30 #Python
python实现xml转json文件的示例代码
Dec 30 #Python
python实现KNN近邻算法
Dec 30 #Python
python 实现逻辑回归
Dec 30 #Python
You might like
PHP内核探索:变量概述
2014/01/30 PHP
php 无限级分类,超级简单的无限级分类,支持输出树状图
2014/06/29 PHP
php的RSA加密解密算法原理与用法分析
2020/01/23 PHP
IE本地存储userdata的一个bug说明
2010/07/01 Javascript
jquery创建并行对象或者合并对象的实现代码
2012/10/10 Javascript
js控制元素显示在屏幕固定位置及监听屏幕高度变化的方法
2015/08/11 Javascript
在线引用最新jquery文件的实现方法
2016/08/26 Javascript
JS文件上传神器bootstrap fileinput详解
2021/01/28 Javascript
纯JS实现表单验证实例
2016/12/24 Javascript
angularJS深拷贝详解
2017/03/23 Javascript
Nodejs读取文件时相对路径的正确写法(使用fs模块)
2017/04/27 NodeJs
用纯Node.JS弹出Windows系统消息提示框实例(MessageBox)
2017/05/17 Javascript
JS 60秒后重新发送验证码的实例讲解
2017/07/26 Javascript
微信小程序 按钮滑动的实现方法
2017/09/27 Javascript
十分钟带你快速了解React16新特性
2017/11/10 Javascript
使用Vue-Router 2实现路由功能实例详解
2017/11/14 Javascript
vue 源码解析之虚拟Dom-render
2019/08/26 Javascript
JS document form表单元素操作完整示例
2020/01/13 Javascript
js 使用ajax设置和获取自定义header信息的方法小结
2020/03/12 Javascript
解决antd的Form组件setFieldsValue的警告问题
2020/10/29 Javascript
[02:47]DOTA2亚洲邀请赛 HR战队出场宣传片
2015/02/07 DOTA
Django在Win7下的安装及创建项目hello word简明教程
2014/07/14 Python
由浅入深讲解python中的yield与generator
2017/04/05 Python
Python实现的排列组合计算操作示例
2017/10/13 Python
python 3利用Dlib 19.7实现摄像头人脸检测特征点标定
2018/02/26 Python
jupyter notebook引用from pyecharts.charts import Bar运行报错
2020/04/23 Python
Python发送手机动态验证码代码实例
2020/02/28 Python
Python numpy矩阵处理运算工具用法汇总
2020/07/13 Python
Html5 语法与规则简要概述
2014/07/29 HTML / CSS
安德玛菲律宾官网:Under Armour菲律宾
2020/07/28 全球购物
计算机专业个人求职信范例
2013/09/23 职场文书
英文导游欢迎词
2014/01/11 职场文书
大学生个人自荐信样本
2014/03/02 职场文书
药学职务聘任书
2014/03/29 职场文书
祖国在我心中的演讲稿
2014/05/04 职场文书
小学优秀学生评语
2014/12/29 职场文书