我们的main.py文件,也就是我们的主函数
#我们的主函数 import StudentEntity from DataAccess import * students = [] def menu(): print('-' * 40) print('通讯录管理系统V1.0') print('1、添加学员信息') print('2、删除学员信息') print('3、打印学员信息') print('4、退出系统') print('-' * 40) #添加学生信息 def add(students): num = input("请输入学员学号:") name = input("请输入学员姓名:") age = input("请输入学员年龄:") contact = input("请输入学员电话:") student = Student(num,name, age,contact) students.append(student) print("学号为{}的{}同学添加成功!".format(student.num,student.name)) def remove(): num=input("请输入你想删除的学生学号:") for i in students: if i.num == num: students.remove(i) print("{}同学被删除,学号为{}".format(i.name,i.num)) break else: print("没有找到学号为{}的学生。".format(i.num)) if __name__ =="__main__": students= loadStudents() while True : menu() op = int(input()) if op == 1: add(students) elif op==2: remove() elif op== 3: for i in students: print(i) elif op == 4: print("感谢您使用学生通讯录管理系统V3.0") break else : print("输入错误,寻找不到对应的功能,请重新输入") saveStudents(students)
我们的DataAccess.py
#用于编辑 from StudentEntity import Student def loadStudents(): file = open("student.txt","r",encoding="utf-8") lines= file.readlines() students = [] for line in lines : a=line.split(",") students.append(Student(a[0],a[1],int(a[2]),a[3].split("/n"))) file.close() return students def saveStudents(students): file = open ("student.txt","w",encoding="utf-8") for i in students: file.write(str(i)+"n") file.close() if __name__=="main": a = loadStudents() print(a) a.append(Student("2024","zhaoliu",22,123456789)) print(a)
我们的StudentEntity.py
class Student: def __init__(self,num,name,age,contact): self.num = num self.name = name self.age = age self.contact = contact def __str__(self): return "{},{},{},{}".format(self.num,self.name,self.age,self.contact) if __name__=="_main_": li= Student("2021","李四",20,134342424) print(li)
这三个文件要放在同一文件夹下,且不要忘了创建一个student.txt
三个文件编写的程序在main中被导入,整合,读写,运行,实现了程序的运行
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)