代码如下:
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")
file_object.close()
print("保存成功!")
def Search(stulist,ID):
print("学号\t姓名\t语文\t数学\t英语\t总分")
count=0
for item in stulist:
if item.ID==ID:
print(item.ID,'\t',
item.name,'\t',
item.score1,'\t',
item.score2,'\t',
item.score3,'\t',
item.sum)
break
count=count+1
if count==len(stulist):
print("没有该学生学号!")
def Del(stulist,ID):
count=0
for item in stulist:
if item.ID==ID:
stulist.remove(item)
print("删除成功!")
break
count +=1
if count == len(stulist):
print("没有该学生学号!")
file_object = open("students.txt","w")
for stu in stulist:
print(stu.name,stu.ID,stu.score1,stu.score2,stu.score3,stu.sum)
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")
file_object.close()
def Change(stulist,ID):
count = 0
for item in stulist:
if item.ID==ID:
stulist.remove(item)
file_object=open("students.txt","w")
for stu in stulist:
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")
file_object.close()
stu=Student()
stu.name=input("请输入学生的姓名")
stu.ID=input("请输入学生的ID")
stu.score1=int(input("请输入学生语文成绩"))
stu.score2=int(input("请输入学生数学成绩"))
stu.score3=int(input("请输入学生英语成绩"))
stu.sumscore()
Add(stulist,stu)
def display(stulist):
print("学号\t姓名 语文 数学 英语 总分")
for item in stulist:
print("%5s %5s %3d %3d %3d %4d"
%(item.ID,item.name,item.score1,item.score2,item.score3,item.sum))
def Sort(stulist):
insertSort(stulist)
display(stulist)
def insertSort(stulist):
for i in range(len(stulist)-1):
for j in range(i+1,len(stulist)):
if stulist[i].sum
效果如下: