Python简易学生管理系统
Project
- main.py
- manager.py
- student.py
- students_info.json
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,self,学生,content,简易,json,student,print
From: https://www.cnblogs.com/mengdie1978/p/17374900.html