开发环境:Windows 7
开发工具:pycharm
开发语言:python3.7
import datetime
class Student():
'''
清华大学
'''
students = []
def __init__(self,name='',age='',sex='',math=0,python=0,num =''):
self.name = name
self.age = age
self.sex = sex
self.math = math
self.python = python
self.num = num
def __str__(self):
msg = "学生信息:name=%s, age=%s, sex=%s, math=%s, python=%s, num=%s" % (self.name,self.age,self.sex,self.math,self.python,self.num)
return msg
#初始化学员信息并保存到文件中,即添加几条默认数据
def init_students(self):
array = [
['小米',18,'女 ',78,76,180815],
['小红',19,'女',87,68,180817],
['小明',18,'男',98,89,180801],
['小花',19,'女',67,68,180805],
['小霞',17,'女',76,87,180809]]
with open('write_data.txt','w', encoding='utf-8') as f:
for row in array:
f.write('%s,%s,%s,%s,%s,%s'%(row[0],row[1],row[2],row[3],row[4],row[5]))
f.write('\n')
self.read_students_from_file()
def read_students_from_file(self):
with open('write_data.txt','r', encoding='utf-8') as f:
for s in f:
stu = s.split(',')
self.students.append(Student(stu[0],stu[1],stu[2],stu[3],stu[4],stu[5]))
def write_students_to_file(self):
with open('write_data.txt','w', encoding='utf-8') as f:
for stu in self.students:
f.write('%s,%s,%s,%s,%s,%s'%(stu.name,stu.age,stu.sex,stu.math,stu.python,stu.num))
f.write('\n')
def check_stuno(self,num):
no_exists = False
for stu in self.students:
if num == stu.num:
no_exists = True
break
return no_exists
def add_student(self):
num = input("请输入要添加的学员学号:")
while self.check_stuno(num):
num = input("该学号已存在,请重新输入:")
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
sex = input("请输入学生性别:")
math = input("请输入学生数学成绩:")
python = input("请输入学生python成绩:")
stu = Student(name,age,sex,math,python,num)
self.students.append(stu)
self.write_students_to_file()
def remove_student(self):
num = input("请输入要删除的学员学号:")
for stu in self.students:
if stu.num == num:
self.students.remove(stu)
print('学员%s已被删除'%num)
self.write_students_to_file()
break
else:
print('学员不存在')
def update_student(self):
num = input("请输入要修改的学员学号:")
for stu in self.students:
if num == stu.num:
stu.name = input("请输入学生姓名:")
stu.age = input("请输入学生年龄:")
stu.sex = input("请输入学生性别:")
stu.math = input("请输入学生数学成绩:")
stu.python = input("请输入学生python成绩:")
self.students[self.students.index(stu)] = stu
self.write_students_to_file()
print('修改成功')
break
else:
print("学员不存在")
def select_student(self):
num = input("请输入要查询的学员学号:")
for stu in self.students:
if num == stu.num:
print("查询到的学生信息:",stu)
break
else:
print("学员不存在")
def print_student_info(self):
for stu in self.students:
print(stu)
if __name__ == "__main__":
day = datetime.datetime.now()
print("---------------------------------------------")
print("当前的时间:",day)
print("---------------------------------------------")
print("欢迎登录学生管理系统")
print("***************************")
stu = Student()
stu.init_students()#添加默认数据
while True:
step = input(
"""====================================
1.添加学生信息
2.删除学生信息
3.修改学生信息
4.查询学生信息
5.显示所有学员信息
6.退出系统
====================================
请选择操作:""")
step = int(step)
if step == 1:
stu.add_student()
stu.print_student_info()
elif step == 2:
stu.remove_student()
stu.print_student_info()
elif step == 3:
stu.update_student()
stu.print_student_info()
elif step == 4:
stu.select_student()
elif step == 5:
stu.print_student_info()
elif step == 6:
print('退出系统')
break
else:
print('指令有误,请重新输入')