Now there are the following requirements:
''' 实现这样的一个功能: 对一个班级的学生的成绩做出一些评定,评定规则是: one: [0-60) -- F two: [60-70) -- D three: [70-80) -- C four: [80-90) -- B five: [90-100] -- A '''
bisect in python can achieve the above requirements
Operating effect:
#python bisect ''' 实现这样的一个功能: 对一个班级的学生的成绩做出一些评定,评定规则是: one: [0-60) -- F two: [60-70) -- D three: [70-80) -- C four: [80-90) -- B five: [90-100] -- A ######################################### 你很可能先想到使用:if....else... 或者想到使用:switch...(java) ########################################## 下面给出不使用以上两种方式实现这一功能 ''' import random import bisect def create_student_scores(n): #根据学生人数n,创建学生成绩 if n >= 0: scores = [] for x in range(n): scores.append(random.randrange(0, 101, 1)) return scores else: print('the number should be greater than 0!') def grade(score, breakpoints = [60, 70, 80, 90], grades = 'FDCBA'): i = bisect.bisect(breakpoints, score) return grades[i] def main(): student_scores = create_student_scores(10) student_results = [grade(score) for score in student_scores] print('学生成绩:{}\n评定结果:{}'.format(student_scores, student_results)) if __name__ == '__main__': main()