博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 入门教程 10 ---- Student Becomes the Teacher
阅读量:6263 次
发布时间:2019-06-22

本文共 7009 字,大约阅读时间需要 23 分钟。

 第一节

     1 练习

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

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

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

 

# three dictionarieslloyd = {"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 herestudents = [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 herestudents = [lloyd , alice , tyler]# you code hrrefor 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_averagedef 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_averagedef get_average(stu):    return 0.1*average(stu["homework"])+0.3*average(stu["quizzes"])+0.6*average(stu["tests"])# function get_letter_gradedef 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_averagedef get_average(stu):    return 0.1*average(stu["homework"])+0.3*average(stu["quizzes"])+0.6*average(stu["tests"])# function get_letter_gradedef 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_averagedef 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 liststudents = [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_averagedef get_average(stu):    return 0.1*average(stu["homework"])+0.3*average(stu["quizzes"])+0.6*average(stu["tests"])# function get_letter_gradedef 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_averagedef 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 resultscore = get_class_average(students)print scoreprint get_letter_grade(score

 

 

转载地址:http://bazpa.baihongyu.com/

你可能感兴趣的文章
linux lftp命令
查看>>
多继承同名隐藏举例
查看>>
sql server 数据库忘记sa账户密码/ 无管理员账户解决办法
查看>>
试玩 PHP 5.4 的新特性
查看>>
Word该值小于列表中的前一条目
查看>>
第九周项目7-趣味编程
查看>>
JavaScript 函数式编程中的 curry 实现
查看>>
21.4 windows_21_Library_use_DLL 动态库补充4
查看>>
查看Eclipse运行工程时使用的Command Line
查看>>
使用WinExec打开文件夹
查看>>
作业要求 20181009-9 每周例行报告
查看>>
Mininet添加iperfmulti功能
查看>>
jq删除标签中的元素
查看>>
从Hash Killer I、II、III论字符串哈希
查看>>
远程使用tomcat8的首页的管理工具
查看>>
打包zip下载
查看>>
js正则表达式
查看>>
带有WS-Security验证的webservice
查看>>
【模式匹配】更快的Boyer-Moore算法
查看>>
[xsy3343]程序锁
查看>>