使用python代码实现 1.录入每个学生基本信息:支持单个录入、批量录入。学生基本信息包括:学号、姓名、性别、身份证号、院系、班级、专业、籍贯、手机、E_mail、QQ等信息。2.如何实现对数据的校验:如学号、身份证号、手机号的检查、以及其他信息的检查,确保录入的数据正确有效。3.数据录入后能够保存,以后查询时可以调入查询。
时间: 2023-08-08 08:07:01 浏览: 147
python实现学生信息管理系统
以下是一个简单的学生信息管理系统的Python代码实现,包括基本信息的录入、数据校验和保存、查询等功能:
```python
import re
import json
class Student:
def __init__(self, id, name, gender, idcard, xueyuan, banji, zhuanye, jiguan, phone, email, qq):
self.id = id
self.name = name
self.gender = gender
self.idcard = idcard
self.xueyuan = xueyuan
self.banji = banji
self.zhuanye = zhuanye
self.jiguan = jiguan
self.phone = phone
self.email = email
self.qq = qq
def __str__(self):
return json.dumps(self.__dict__)
class StudentManager:
def __init__(self):
self.students = []
self.id_set = set()
self.phone_set = set()
self.idcard_set = set()
def add_student(self, student):
if student.id in self.id_set:
print("Error: 学号已存在")
return False
if student.phone in self.phone_set:
print("Error: 手机号已存在")
return False
if not self.check_idcard(student.idcard):
print("Error: 身份证号格式不正确")
return False
if student.idcard in self.idcard_set:
print("Error: 身份证号已存在")
return False
self.students.append(student)
self.id_set.add(student.id)
self.phone_set.add(student.phone)
self.idcard_set.add(student.idcard)
return True
def add_students(self, students):
success = 0
for student in students:
if self.add_student(student):
success += 1
print("成功录入{}个学生信息".format(success))
def delete_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
self.id_set.remove(id)
self.phone_set.remove(student.phone)
self.idcard_set.remove(student.idcard)
print("学号为{}的学生已被删除".format(id))
return True
print("Error: 学号不存在")
return False
def query_student(self, id):
for student in self.students:
if student.id == id:
print(str(student))
return True
print("Error: 学号不存在")
return False
def get_all_students(self):
for student in self.students:
print(str(student))
def check_idcard(self, idcard):
pattern = r"\d{17}[\dXx]"
if not re.match(pattern, idcard):
return False
factors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]
check_codes = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"]
total = sum([int(idcard[i]) * factors[i] for i in range(17)])
check_code = check_codes[total % 11]
if idcard[-1].upper() == check_code:
return True
return False
manager = StudentManager()
while True:
print("请选择操作:1-单个录入,2-批量录入,3-查询学生,4-显示所有学生,5-删除学生,0-退出")
choice = input()
if choice == "1":
id = input("请输入学号:")
name = input("请输入姓名:")
gender = input("请输入性别:")
idcard = input("请输入身份证号:")
xueyuan = input("请输入院系:")
banji = input("请输入班级:")
zhuanye = input("请输入专业:")
jiguan = input("请输入籍贯:")
phone = input("请输入手机:")
email = input("请输入E_mail:")
qq = input("请输入QQ:")
student = Student(id, name, gender, idcard, xueyuan, banji, zhuanye, jiguan, phone, email, qq)
manager.add_student(student)
elif choice == "2":
count = int(input("请输入要录入的学生数量:"))
students = []
for i in range(count):
id = input("请输入学号:")
name = input("请输入姓名:")
gender = input("请输入性别:")
idcard = input("请输入身份证号:")
xueyuan = input("请输入院系:")
banji = input("请输入班级:")
zhuanye = input("请输入专业:")
jiguan = input("请输入籍贯:")
phone = input("请输入手机:")
email = input("请输入E_mail:")
qq = input("请输入QQ:")
student = Student(id, name, gender, idcard, xueyuan, banji, zhuanye, jiguan, phone, email, qq)
students.append(student)
manager.add_students(students)
elif choice == "3":
id = input("请输入要查询的学生学号:")
manager.query_student(id)
elif choice == "4":
manager.get_all_students()
elif choice == "5":
id = input("请输入要删除的学生学号:")
manager.delete_student(id)
elif choice == "0":
break
else:
print("Error: 无效操作")
```
在代码中,`Student` 类表示一个学生对象,包含学生的各项基本信息,`StudentManager` 类表示学生信息管理器,包含各种操作学生信息的方法。在录入学生信息时,通过对输入的各项信息进行校验,确保数据的正确有效。同时,通过使用 `json` 模块将学生对象转换为字符串,可以方便地将学生信息保存到文件或数据库中。在查询学生信息时,可以根据学号进行查询,输出查询结果。在删除学生信息时,先根据学号查找学生对象,如果存在则从管理器中删除该学生信息。
阅读全文