1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| SCOREFILE = "scores.xlsx"
NAME = "C"
TOTALSCORE = "M"
CREDIT = "K"
SCOREMAP = { 'A+':98, 'A':92, 'A-':87, 'B+':83, 'B':79, 'B-':76, 'C+':73, 'C':70, 'C-':66, 'D':62, 'F':30, '零分':0, 'A+': 98, 'A-': 87, 'B+': 83, 'B-': 76, 'C+': 73, 'C-': 66, }
from openpyxl import load_workbook
class Course: def __init__(self,score,credit): self.score=score self.credit=credit
class Student: def __init__(self,name): self.name=name self.coursecount = 0 self.average = 0 self.course=[]
def getAverage(self): totalcredit = 0 totalscore = 0 for course in self.course: totalscore = totalscore + course.score*course.credit totalcredit = totalcredit + course.credit self.average = totalscore/totalcredit
return self.average
def readFile(filename=SCOREFILE): scoredata = load_workbook(filename) return scoredata
def getSheet(scoresheet): namelist=[cell.value for cell in scoresheet[NAME]][1:] nameset=set(namelist)
scorelist=[cell.value for cell in scoresheet[TOTALSCORE]][1:]
creditlist=[cell.value for cell in scoresheet[CREDIT]][1:]
studentlist={} for i in nameset: studentlist[i]=Student(i)
for index in range(len(namelist)): if scorelist[index] in SCOREMAP: course = Course(SCOREMAP[scorelist[index]],float(creditlist[index])) elif scorelist[index] == "及格": continue else: course = Course(float(scorelist[index]),float(creditlist[index])) studentlist[namelist[index]].course.append(course)
return studentlist
if __name__ == "__main__":
print("data loading...") scoredata = readFile() major = '计算机' print("ready! \r\n 专业列表:") print("\r\n".join(scoredata.sheetnames))
while(True): major = input("请输入要计算的专业,输入all查看总排名,输入\'exit\'退出程序:\t")
if major == "exit": break
if major == "all": scorelist = []
for stumajor in scoredata.sheetnames: studata = scoredata.get_sheet_by_name(stumajor) stulist = getSheet(studata) scorelist += [(stulist[stu].getAverage(),stulist[stu].name) for stu in stulist]
total = sum([stu[0] for stu in scorelist])
scorelist.sort(reverse=True) for i in range(len(scorelist)): print("{} \t {:.3f}: \t排名 {}".format(scorelist[i][1],scorelist[i][0],i+1))
print("平均分:\t%f" % (total/len(scorelist)))
else: try: studata = scoredata.get_sheet_by_name(major) except: print("没有这个专业,请重新输入") continue
stulist = getSheet(studata) scorelist = [(stulist[stu].getAverage(),stulist[stu].name) for stu in stulist] scorelist.sort(reverse=True)
total = 0
for i in range(len(scorelist)): total += scorelist[i][0]
print("{:.3f}: \t排名 {}".format(scorelist[i][0], i + 1))
print("平均分:\t%f" % (total/len(scorelist)))
|