• Python 入门教程 10 ---- Student Becomes the Teacher



     第一节

         1 练习

            1 设置三个的字典分别为lloyd,alice,tyler

            2 对每一个的字典的key都设置为"name","homework" , "quizzes", "tests"

            3 每个字典的key为"name"对应的为人的名字,其他key对应的值为空列表

    # three dictionaries
    lloyd = {"name":"Lloyd" , "homework":[] , "quizzes":[] , "tests":[]}
    alice = {"name":"Alice" , "homework":[] , "quizzes":[] , "tests":[]}
    tyler = {"name":"Tyler" , "homework":[] , "quizzes":[] , "tests":[]}


     第二节

         1 练习:把第一个字典lloyd中的三个空列表的值设置对应如下

                        Homework: 90.0, 97.0, 75.0, 92.0
                        Quizzes: 88.0, 40.0, 94.0
                        Test Scores: 75.0, 90.0

    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]
    }


     第三节

         1 练习:把lloyd , alice , tyler三个字典放到列表students中

    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]
    }
    # you code here
    students = [lloyd , alice , tyler]


     第四节

         1 练习:按照以下的格式,打印出students列表中的三个字典

    Lloyd
    [90, 97, 75, 92]
    [88, 40, 94]
    [75, 90]
    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]
    }
    # you code here
    students = [lloyd , alice , tyler]
    # you code hrre
    for my_list in students:
        for str in my_list:
            print my_list[str]
    


     第五节

          1 练习

             1 对于每一个数字列表,我们可以求其平均值,比如    average([0]) #0.0,average([0,2]) #1.0,average([0,1]) #0.5

             2 写一个函数average,参数是列表lst,求其平均值并以float形式输出

    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]
    }
    
    # Add your function below!
    def average(lst):
        sum = 0
        for num in lst:
            sum = sum+num
        return float(sum)/len(lst)
    


     第六节

         1 介绍了我们经常需要去求很多数的平均值,比如80 * 0.4 + 86 * 0.6

         2 练习:写一个函数get_average,参数是一个学生字典,使用如下比例Homework  10%, quizzes 30% tests60%算出这个学生的平均得分

    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]
    }
    
    # Add your function below!
    def average(lst):
        sum = 0
        for num in lst:
            sum = sum+num
        return float(sum)/len(lst)
    # function get_average
    def get_average(stu):
        return 0.1*average(stu["homework"])+0.3*average(stu["quizzes"])+0.6*average(stu["tests"])
     


     第七节

          1 练习

              1 写一个函数get_letter_grade,参数是score,按照以下的规则返回对应的等级

    Scores 90 or above: return "A"
    If 80 <= score < 90: return "B"
    If 70 <= score < 80: return "C"
    If 60 <= score < 70: return "D"
    If score < 60: return "F"
    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]
    }
    
    # Add your function below!
    def average(lst):
        sum = 0
        for num in lst:
            sum = sum+num
        return float(sum)/len(lst)
    
    # function get_average
    def get_average(stu):
        return 0.1*average(stu["homework"])+0.3*average(stu["quizzes"])+0.6*average(stu["tests"])
    
    # function get_letter_grade
    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"
    


     第八节

         1 练习:写一个函数get_class_average,参数是班级的列表,求出这个班级所有人的平均值

    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]
    }
    
    # Add your function below!
    def average(lst):
        sum = 0
        for num in lst:
            sum = sum+num
        return float(sum)/len(lst)
    
    # function get_average
    def get_average(stu):
        return 0.1*average(stu["homework"])+0.3*average(stu["quizzes"])+0.6*average(stu["tests"])
    
    # function get_letter_grade
    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"
    
    # function get_class_average
    def get_class_average(class_list):
        sum = 0
        for stu in class_list:
            sum = sum + get_average(stu)
        return float(sum)/len(class_list)
        

     

     第十节

         1 练习:打印出students这个班级列表的平均值,然后利用这个值求平均等级

    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 list
    students = [lloyd , alice , tyler]
    
    # Add your function below!
    def average(lst):
        sum = 0
        for num in lst:
            sum = sum+num
        return float(sum)/len(lst)
    
    # function get_average
    def get_average(stu):
        return 0.1*average(stu["homework"])+0.3*average(stu["quizzes"])+0.6*average(stu["tests"])
    
    # function get_letter_grade
    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"
    
    # function get_class_average
    def get_class_average(class_list):
        sum = 0
        for stu in class_list:
            sum = sum + get_average(stu)
        return float(sum)/len(class_list)
    
    # print you result
    score = get_class_average(students)
    print score
    print get_letter_grade(score
    
    



  • 相关阅读:
    [LeetCode] 139. Word Break 单词拆分
    [LeetCode] 140. Word Break II 单词拆分II
    [LeetCode] 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化
    [LeetCode] 206. Reverse Linked List 反向链表
    [LeetCode] 92. Reverse Linked List II 反向链表II
    [LeetCode] 258. Add Digits 加数字
    [LeetCode] 66. Plus One 加一
    [LeetCode] 21. Merge Two Sorted Lists 合并有序链表
    [LeetCode] 88. Merge Sorted Array 合并有序数组
    [LeetCode] 10. Regular Expression Matching 正则表达式匹配
  • 原文地址:https://www.cnblogs.com/james1207/p/3343386.html
走看看 - 开发者的网上家园