pyhthon 求GPA平均学分绩点
1 #coding=utf-8 2 ''' 3 北大4.0 4 成绩 学分 5 100~90 4.0 6 89~85 3.7 7 84~82 3.3 8 81~78 3.0 9 77~75 2.7 10 74~72 2.3 11 71~68 2.0 12 67~64 1.5 13 63~60 1.0 14 59~0 0 15 平均学分绩点=∑(课程学分×成绩绩点)/∑课程学分=各门课程学分绩点之和/各门课程学分数之和 16 ''' 17 filename='./myscore.txt' 18 list1=[] 19 def cal(): 20 fr=open(filename) 21 for line in fr.readlines(): 22 line1=line.strip("\n").split(",") 23 # print(line1) 24 a=float(line1[0]) 25 b=float(line1[1]) 26 if(a>=90): 27 list1.append((a,(b,4.0))) 28 if(a>=85 and a<=89): 29 list1.append((a,(b,3.7))) 30 if(a>=82 and a<=84): 31 list1.append((a,(b,3.3))) 32 if(a>=78 and a<=81): 33 list1.append((a,(b,3.0))) 34 if(a>=75 and a<=77): 35 list1.append((a,(b,2.7))) 36 if(a>=72 and a<=74): 37 list1.append((a,(b,2.3))) 38 if(a>=68 and a<=71): 39 list1.append((a,(b,2.0))) 40 if(a>=64 and a<=67): 41 list1.append((a,(b,1.5))) 42 if(a>=60 and a<=63): 43 list1.append((a,(b,1.0))) 44 if(a<60): 45 list1.append((a,(b,0.0))) 46 47 # print(list1) 48 # print(list1.keys()) 49 # print(list1.values()) 50 c=list1 51 sum_p=0 52 sum_s=0 53 # print(c) 54 for i in range(len(c)): 55 sum_p=sum_p+c[i][1][0]*c[i][1][1] 56 sum_s=sum_s+c[i][1][0] 57 # print(sum_p) 58 # print(sum_s) 59 result=sum_p/sum_s 60 print("北大4.0(GPA):%.2f"%result) 61 62 if __name__ == '__main__': 63 cal()