You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB

import random
class Student:
def __init__(self, name, sno, sex, age):
self.name = name
self.sno = sno
self.sex = sex
self.age = age
self.score = dict()
self.total_score = 0
def update_score(self, course, score):
self.score[course] = score
self.total_score += score
def get_avg(self):
return self.total_score / len(self.score)
if __name__ == "__main__":
stu = dict()
with open('data.csv', 'r') as f:
lines = f.readlines()
for index, l in enumerate(lines):
if index == 0:
continue
w = l.split(',')
if len(w) > 1:
s = Student(w[1], w[0], w[2], w[3])
stu[w[1]] = s
with open('score.csv', 'r') as f:
lines = f.readlines()
for index, l in enumerate(lines):
if index == 0:
continue
w = l.split(',')
if len(w) > 1:
stu[w[1]].update_score(w[2], eval(w[3]))
stu = list(stu.values())
stu.sort(key=lambda x : x.get_avg())
for s in stu:
print("name:{}, avg:{}".format(s.name, s.get_avg()))