python開發bisect

來源:互聯網
上載者:User
現在有如下的需求:

'''    實現這樣的一個功能:    對一個班級的學生的成績做出一些評定,評定規則是:    one: [0-60)     -- F    two: [60-70)    -- D    three: [70-80)  -- C    four: [80-90)   -- B    five: [90-100]  -- A'''

python中的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 randomimport bisectdef 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()
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.