The first thing of course is to generate students' scores, here is my own randomly generated, direct look at the code, written comments are very detailed
. 1 Import Random 2 Import JSON . 3 . 4 . 5 DEF random_score (SUM, bottom, Top): . 6 '' ' . 7 : param SUM: Total generating a random number . 8 : param bottom: the lower bound of the random number . 9 : param Top: random number streets 10 : return: score_list random number list . 11 '' ' 12 is score_list = [] 13 is for I in Range (SUM): 14 # generates pseudo-random integer with randint random library 15 random_number = the random.randint (bottom, Top) 16 score_list.append (random_number) . 17 18 is return score_list . 19 20 is 21 is DEF write_file (filename, tmp_list): 22 is '' ' 23 is : param filename: write the file name 24 : param tmp_list: Incoming write file contents 25 : return: 26 is ' '' 27 with Open (filename, ' W ' ) AS F: 28 # The json.dump () can be used to encode JSON data, and writes the file stream 29 The json.dump (tmp_list, F) 30 31 is 32 DEF main () : 33 # primary function 34 score_list = random_score(40, 50, 100) 35 filename = 'students_score' 36 write_file(filename, score_list) 37 38 39 if __name__ == '__main__': 40 main()
The second step is to calculate all kinds of friends, notes written in a very full also not resolved
. 1 Import JSON 2 Import Math . 3 Import numpy . 4 . 5 . 6 DEF the read_file (filename): . 7 "" " . 8 : param filename: read file name . 9 : return: 10 " "" . 11 12 is with Open (filename, ' R & lt ' ) F aS: 13 is # the json.load json can decode the data type to a corresponding file 14 score_list = the json.load (F) 15 return score_list 16 . 17 18 is DEF cal_score (score_list): 19 "" " 20 21 : param score_list: All students score 22 : return: 23 " "" 24- 25 # list itself is a function of the maximum find a minimum of 26 max_score = max (score_list) 27 min_score = min (score_list) 28 29 # with a math FSUM () for obtaining the sum and then averaged, the simple method 30 AVERAGE_SCORE = math.fsum (score_list) / len (score_list) 31 is # AVERAGE_SCORE = numpy.mean (score_list) may be, in one step 32 33 is # lazy using a median numpy seeking function, and the most common method is to find the middle of the list or two intermediate averaging 34 is middle_score =numpy.median (score_list) 35 36 # Here I use a list of analytic methods to find the score for each satisfy the condition 37 [ Fail = [Score for Score in score_list IF Score <60 ] 38 is d_class = [Score for Score in score_list IF 60 <= Score <70 ] 39 c_class = [Score for Score in score_list IF 70 <= Score <80 ] 40 b_class = [Score for Score in score_list IF 80 <= Score <90 ] 41 is a_class = [Score for Score in score_list IF 90 <= Score <100 ] 42 is 43 is # numpy.std () standard deviation of the required number of all within the list of 44 is of standard_deviation = numpy.std (score_list) 45 # where the number of bits is too standard deviation long, using round () at the accuracy of the control, a first parameter is the count of incoming long tail, the second parameter is a decimal precision control 46 is ctl_st_dev = round (a float (of standard_deviation), 2 ) 47 48 Print ( " the test scores as follows: " ) 49 # the format () normalized output 50 Print ( " average: {}, the highest score: {}, the lowest point: {}, median score: {}, standard deviation scores: {} (two decimal places)" .Format (AVERAGE_SCORE, 51 is max_score, min_score, middle_score, ctl_st_dev)) 52 is Print ( " number of points of each segment are as follows: " ) 53 is Print ( " fail Number: {} people, 60-70 min: People {} , 70-80 min: {} people, 80-90 min: people {} " 54 " , 90-100 points: {} person " .format (Fail, len (d_class), len (c_class), len (b_class), len (a_class))) 55 56 is 57 is DEF main (): 58 score_list the read_file = ( ' students_score ' ) 59 cal_score (score_list) 60 61 62 if __name__ == '__main__': 63 main()