Home > Backend Development > Python Tutorial > python development bisect

python development bisect

高洛峰
Release: 2016-12-14 15:45:12
Original
1555 people have browsed it

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
'''
Copy after login

bisect in python can achieve the above requirements

Operating effect:

python development bisect

#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()
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template