Python development _bisect

Now the following requirements:

'''
    To achieve such a function: 
    On a class of students to make some assessment, assessment rules: 
    one: [0-60)     -- F
    two: [60-70)    -- D
    three: [70-80)  -- C
    four: [80-90)   -- B
    five: [90-100]  -- A
'''

pythonThe bisect can achieve the above requirements

Effect of operation:

====================================================

Part of the code:

====================================================

 1 #python bisect
 2 '''
 3     To achieve such a function: 
 4     On a class of students to make some assessment, assessment rules: 
 5     one: [0-60)     -- F
 6     two: [60-70)    -- D
 7     three: [70-80)  -- C
 8     four: [80-90)   -- B
 9     five: [90-100]  -- A
10     #########################################
11     You might first think of use: if....else...
12     Or think of using: switch...(java)
13     ##########################################
14     Here are not using the above two ways to achieve this function
15 '''
16 
17 import random
18 import bisect
19 
20 def create_student_scores(n):
21     #According to the number of students n, create student achievement
22     if n >= 0:
23         scores = []
24         for x in range(n):
25             scores.append(random.randrange(0, 101, 1))
26         return scores
27     else:
28         print('the number should be greater than 0!')
29     
30 
31 def grade(score, breakpoints = [60, 70, 80, 90], grades = 'FDCBA'):
32     i = bisect.bisect(breakpoints, score)
33     return grades[i]
34 
35 def main():
36     student_scores = create_student_scores(10)
37     student_results = [grade(score) for score in student_scores]
38     print('Student achievement assessment results: {}\n:{}'.format(student_scores, student_results))
39 
40 if __name__ == '__main__':
41     main()

Posted by Tyler at November 19, 2013 - 8:14 AM