Title: Calculate the average of Chinese scores in Student_grade.txt and find out who is the highest student in mathematics
Method One:
With open (' Student_grade.txt ', ' R ', encoding= ' utf-8 ') as f: data = F.read () #读取student_grade. txt content to data (text) Data_lines=data.splitlines () #把文本转换成列表 while removing the newline empty={} #定义一个空字典for i in Data_lines: data_list=i.split ( ' \ t ') empty[data_list[0]]=data_list[1:] #遍历列表data_lines中的每个元素, remove ' \ t ' after generating a dictionary of names and scores. Empty.pop (' name ') #弹出标题行shuxue =0yuwen=0for k,v in Empty.items (): yuwen=yuwen+int (v[0]) #遍历字典, the sum of the language results if int (v[1]) > Shuxue: shuxue=int (v[1]) #遍历字典empty, to find the highest mathematical results name=kprint (name+ ' classmate of the highest mathematical performance, is%d. ') %shuxue, ' \ n ', ' The average language score is%d. '% ( Yuwen/len (empty)))
Calculate the average of the language scores in Student_grade.txt, and find out who is the student with the highest math score