import random
CreditsList = {'高数': 5, '制图': 3.5, '英语': 4, 'Python': 1.5}
class Credits:
def __init__(self):
self.sredits = {'高数': random.randint(60, 100),
'制图': random.randint(60, 100),
'英语': random.randint(60, 100),
'Python': random.randint(60, 100)}
def ave_4(data):
totalCredits = sum(CreditsList.values())
total = 0
for k, v in data.sredits.items():
total += v*CreditsList[k]
return total/totalCredits
students = []
for i in range(10):
name = '学生'+str(i)
data = Credits()
print(name, data.sredits)
students.append([name, data])
# 输出加权平均成绩大于80分的学生
for student in students:
if ave_4(student[1]) > 80:
print(student[0])
#按加权平均成绩从高到底排序,输出排名榜单
for student in students:
student.append(ave_4(student[1])) # 将加权平均成绩添加到学生信息列表中
students_sort = [i[0] for i in sorted(students, key=lambda x: x[2])] # 按照成绩排序每组数据的第3个,然后取每组数据的第一个
print(students_sort)