1. 程式人生 > >pyhthon 求GPA平均學分績點

pyhthon 求GPA平均學分績點

list odin main value values readline result sco pen

 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()

pyhthon 求GPA平均學分績點