import os
class Student: #定义一个学生类
def __init__(self):
self.name = ''
self.ID = ''
self.score1 = 0
self.score2 = 0
self.score3 = 0
self.sum = 0
def sumscore(self): #计算总分
self.sum=self.score1 + self.score2 +self.score3
#----------------
def searchByID(stulist,ID): #按学号查找看是否学号已经存在
for item in stulist:
if item.ID == ID:
return True
def Add(stulist,stu): #添加一个学生信息
if searchByID(stulist,stu.ID) == True:
print('学号已存在!')
return False
stulist.append(stu)
print(stu.name,stu.ID,stu.score1,stu.score2,stu.score3,stu.sum)
print('是否要保存学生信息?')
nChoose = input('Choose Y/N')
if nChoose == 'Y' or nChoose == 'y':
file_object = open('students.txt','a')
file_object.write(stu.ID)
file_object.write('')
file_object.write(stu.name)
file_object.write('')
file_object.write(str(stu.score1))
file_object.write('')
file_object.write(str(stu.score2))
file_object.write('')
file_object.write(str(stu.score3))
file_object.write('')
file_object.write(str(stu.sum))
file_object.write('\n')
fil