''' 每一個學生的總分,每個課程的平均分,最高分,最低分 ''' # 創建學生列表 stuLst = [] # 創建學生信息 stu1 = {'學號':'1001','姓名':'小明','高數':95,'英語':88,'計算機':80} stu2 = {'學號':'1002','姓名':'小李','高數':84,'英語':70,'計算機':60} stu3 = {'學號':'1003','姓名':'小王','高數':79,'英語':78,'計算機':75} # 將學生列表加入到學生信息中 stuLst.append(stu1) stuLst.append(stu2) stuLst.append(stu3) def sumScore(stuLst): '''計算每名學生的總分''' for stu in stuLst: print(stu['姓名'],"的三科總分是 ",stu['高數'] + stu['英語'] + stu['計算機']) def meanScore(stuLst): '''計算課程的平均分''' sumProjectScore_gs = 0 # 設置高數學科總分 sumProjectScore_yy = 0 # 設置英語學科總分 sumProjectScore_jsj = 0 # 設置計算機學科總分(_拼音縮寫) for stu in stuLst: sumProjectScore_gs += stu['高數'] sumProjectScore_yy += stu['英語'] sumProjectScore_jsj += stu['計算機'] print("高數的平均分是 %.2f"%(sumProjectScore_gs//len(stuLst))) print("英語的平均分是 %.2f" % (sumProjectScore_yy // len(stuLst))) print("計算機的平均分是 %.2f" % (sumProjectScore_jsj // len(stuLst))) def maxScore(stuLst): '''求最大值''' # 高數 英語 計算機 gs = [] yy = [] jsj = [] for stu in stuLst: gs.append(stu['高數']) yy.append(stu['英語']) jsj.append(stu['計算機']) print("高數的最高分是 %.2f"%(max(gs))) print("英語的最高分是 %.2f" % (max(yy))) print("計算機的最高分是 %.2f" % (max(jsj))) def minScore(stuLst): '''求最小值''' # 高數 英語 計算機 gs = [] yy = [] jsj = [] for stu in stuLst: gs.append(stu['高數']) yy.append(stu['英語']) jsj.append(stu['計算機']) print("高數的最低分是 %.2f" % (min(gs))) print("英語的最低分是 %.2f" % (min(yy))) print("計算機的最低分是 %.2f" % (min(jsj))) sumScore(stuLst) meanScore(stuLst) maxScore(stuLst) minScore(stuLst)
2020-05-22