Python简易学生管理系统
Posted 映辉博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python简易学生管理系统相关的知识,希望对你有一定的参考价值。
目录结构:
1、 学生文件 student.py
# 学生类 class Student(object): # 存放学生信息 student_info = # 学生初始化方法 def __init__(self,id,name,addr,tel): self.id = id self.name = name self.addr = addr self.tel = tel # 将学生的信息添加到学生信息字典里 self.student_info["学号"] = self.id self.student_info["姓名"] = self.name self.student_info["地址"] = self.addr self.student_info["电话"] = self.tel
2、 管理系统文件 manager.py
import json import student # 管理系统类 class Manager(object): # 展示所有学生信息的方法 def show_all_student(self): with open("students_info.json", mode="r", encoding="utf8") as f: content = json.loads(f.read()) if content: print("所有的学生信息如下:") print("#######################################") for stu in content: print(stu) print("#######################################") elif len(content) == 0: print("Sorry! 一个学生都没有!") # 添加学生的方法 def add_student(self): print("~~~~~~~~~~~~~~~~~~~~将要开始添加学生信息~~~~~~~~~~~~~~~~~~~~~") id = input("请输入学号:") name = input("请输入姓名:") addr = input("请输入地址:") tel = input("请输入电话:") new_student = student.Student(id, name, addr, tel) with open("students_info.json", mode="r", encoding="utf8") as f1: content = json.loads(f1.read()) new_content = list(content) new_content.append(new_student.student_info) json_content = json.dumps(new_content) with open("students_info.json",mode="w",encoding="utf8") as f2: f2.write(json_content) print("~~~~~~~~~~~~~~~~~~~~!!!添加完成!!!~~~~~~~~~~~~~~~~~~~~~") # 删除学生的方法 def delete_student(self): print("~~~~~~~~~~~~~~~~~~~~将要开始添加学生信息~~~~~~~~~~~~~~~~~~~~~") id = input("请输入要删除的学生的学号:") with open("students_info.json",mode="r",encoding="utf8") as f: content = list(json.loads(f.read())) for item in content: if item["学号"] == id: content.remove(item) json_content = json.dumps(content) with open("students_info.json", mode="w", encoding="utf8") as f2: f2.write(json_content) print("~~~~~~~~~~~~~~~~~~~~!!!删除成功!!!~~~~~~~~~~~~~~~~~~~~~") return print("输入的学号不存在!!!") # 修改学生的方法 def update_student(self): print("~~~~~~~~~~~~~~~~~~~~开始修改学生信息~~~~~~~~~~~~~~~~~~~~~") with open("students_info.json", mode="r", encoding="utf8") as f: text = list(json.loads(f.read())) delete_number = input("请输入要修改的学生学号:") for i in text: if i["学号"] == delete_number: new_addr = input("请输入修改后的地址:") new_tel = input("请输入修改后的电话:") i["地址"] = new_addr i["电话"] = new_tel json_text = json.dumps(text) with open("students_info.json", mode="w", encoding="utf8") as f2: f2.write(json_text) print("~~~~~~~~~~~~~~~~~~~~修改成功~~~~~~~~~~~~~~~~~~~~~") return print("输入的学号不存在!!!!!!!!!!!!!") # 系统启动入口方法 def system_start(self): while True: print("########### 欢迎来到学生管理系统 ############") print("################ 请登录系统 ################") user_name = input("请输入用户名: ") pass_word = input(" 请输入密码: ") if user_name == "zyh" and pass_word == "123456": print("################ 登陆成功!################") break else: print("登录信息错误,请重新登录!") while True: print("################ 你已经进入学生管理系统 ################") print("###################### 请选择功能 ######################") print("1. 展示所有学生信息") print("2. 添加学生") print("3. 删除学生") print("4. 修改学生") print("5. 退出系统") print("#######################################################") while True: choice = int(input("请输入功能对应的序号(1/2/3/4/5/6): ")) if choice == 1: # print("调用展示所有学生信息的方法") self.show_all_student() break elif choice == 2: # print("调用添加学生的方法") self.add_student() break elif choice == 3: # print("调用删除学生的方法") self.delete_student() break elif choice == 4: # print("调用修改学生的方法") self.update_student() break elif choice == 5: print("退出学生管理系统!!!!!!!!!!!") return else: print("输入信息不合法,请重新输入!")
3、 程序入口文件 main.py
import manager if __name__ == \'__main__\': manager_sys = manager.Manager() manager_sys.system_start()
4、 学生信息文件 students_info.json
[ "学号": "0001", "姓名": "张三", "地址": "北京", "电话": "13244453359", "学号": "0002", "姓名": "张三2", "地址": "北京", "电话": "13244453359", "学号": "0003", "姓名": "张三3", "地址": "北京", "电话": "13244453359", "学号": "0004", "姓名": "张三4", "地址": "北京", "电话": "13244453359" ]
用python实现简易学生管理系统
以前用C++和Java写过学生管理系统,也想用Python试试,果然“人生苦短,我用Python”。用Python写的更加简洁,实现雏形也就不到100行代码。
下面上代码
1 #!/usr/bin/python3 2 # coding=utf-8 3 4 5 # 实现switch-case语句 6 class switch(object): 7 def __init__(self, value): 8 self.value = value 9 self.fall = False 10 11 def __iter__(self): 12 """Return the match method once, then stop""" 13 yield self.match 14 raise StopIteration 15 16 def match(self, *args): 17 """Indicate whether or not to enter a case suite""" 18 if self.fall or not args: 19 return True 20 elif self.value in args: # changed for v1.5, see below 21 self.fall = True 22 return True 23 else: 24 return False 25 26 27 class student: 28 def __init__(self, name, age, id, grade): 29 self.next = None 30 self.name = name 31 self.age = age 32 self.id = id 33 self.grade = grade 34 35 def show(self): 36 print(\'name:\', self.name, \' \', \'age:\', self.age, \' \', \'id:\', self.id, \' \', \'grade:\', self.grade) 37 38 39 class stulist: 40 def __init__(self): 41 self.head = student(\'\', 0, 0, 0) 42 43 def display(self): 44 p = self.head.next 45 if p==None: 46 print(\'no student data in database!\') 47 while p: 48 p.show() 49 p = p.next 50 51 52 def insert(self): 53 print(\'please enter:\') 54 name = input(\'name:\') 55 age = input(\'age:\') 56 id = input(\'id:\') 57 grade = input(\'grade:\') 58 newstu = student(name, age, id, grade) 59 p = self.head 60 while p.next: 61 p = p.next 62 p.next = newstu 63 64 def query(self): 65 name = input(\'please enter the name you want to query:\') 66 p = self.head.next 67 if p==None: 68 print(\'no such student in database\') 69 while p: 70 if p.name == name: 71 p.show() 72 p = p.next 73 74 def delete(self): 75 name = input("please enter the student\'name you want to delete:") 76 p = self.head.next 77 pre = self.head 78 while p: 79 if p.name == name: 80 pre.next = p.next 81 p.next = None 82 print(\'the student info has been deleted!\') 83 break 84 else: 85 pre = p 86 p = p.next 87 88 89 def main(): 90 stulist1 = stulist() 91 user_input = input(\'please enter the OPcode:\') 92 while user_input: 93 print(\'a--insert/b--display/c--query/h or \'\'--help/d--delete\') 94 for case in switch(user_input): 95 if case(\'a\'): 96 stulist1.insert() 97 user_input = input(\'please enter the OPcode:\') 98 break 99 if case(\'b\'): 100 stulist1.display() 101 user_input = input(\'please enter the OPcode:\') 102 break 103 if case(\'c\'): 104 stulist1.query() 105 user_input = input(\'please enter the OPcode:\') 106 break 107 if case(\'d\'): 108 stulist1.delete() 109 user_input = input(\'please enter your OPcode:\') 110 break 111 if case(): # default 112 print(\'please enter the OPcode...\') 113 user_input = input(\'please enter the OPcode:\') 114 break 115 116 117 if __name__ == "__main__": 118 main()
下面是运行结果:
以上是关于Python简易学生管理系统的主要内容,如果未能解决你的问题,请参考以下文章