每天進步一點點--------python之計算平均分__python

來源:互聯網
上載者:User

周末的圖書館真是太熱鬧了,好多小孩子圍在一起做作業讓我想起以前讀書的時候也是這樣。剛好這個課後練習就是計算成績的平均分,想來當初老師如果有這玩意應該會輕鬆很多吧,畢竟一個一個算實在是有夠麻煩的。不是很複雜的小指令碼,不過很實用。


lloyd = {    "name": "Lloyd",    "homework": [90.0, 97.0, 75.0, 92.0],    "quizzes": [88.0, 40.0, 94.0],    "tests": [75.0, 90.0]}alice = {    "name": "Alice",    "homework": [100.0, 92.0, 98.0, 100.0],    "quizzes": [82.0, 83.0, 91.0],    "tests": [89.0, 97.0]}tyler = {    "name": "Tyler",    "homework": [0.0, 87.0, 75.0, 22.0],    "quizzes": [0.0, 75.0, 78.0],    "tests": [100.0, 100.0]}students = [lloyd,alice,tyler]def average (lst):                             #計算平均值,精確到小數點    return float(sum(lst))/len(lst)    def get_average(student):                      #按照不同的比例計算平比分,大學經常這樣弄    homework = student['homework']    quiz = student['quizzes']    test = student['tests']    return average(homework)*0.1 +average(quiz)*0.3 + average(test)*0.6    def get_letter_grade(score):                   #按照分數排等級    if score >= 90:        return "A"    elif score >= 80 and score < 90:        return "B"    elif score >= 70 and score < 80:        return "C"    elif score >= 60 and score < 70:        return "D"    else :        return "F"         def get_class_average(class_list):             #計算全班的平均分,先用迴圈計算出總分然後除去人數    total = 0    for i in class_list:        total += get_average(i)    return total/len(class_list)

等基本的知識牢固之後再就是邏輯思維的考驗了,革命尚未成功,同志仍需努力。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.