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

本文共 7062 字,大约阅读时间需要 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

 

 

转载于:https://www.cnblogs.com/james1207/p/3343386.html

你可能感兴趣的文章
淌淌淌
查看>>
BZOJ1002:[FJOI2007]轮状病毒
查看>>
SSD5_Recommended Exercise 4 分析
查看>>
django实现分页功能
查看>>
CSS伪类与CSS伪元素的区别
查看>>
如何:修改字符串内容
查看>>
Tomcat Server之启动---Bootstrap类
查看>>
经典问题-生产者和消费者问题
查看>>
Hadoop Distributed File System 简介
查看>>
文档通信(跨域-不跨域)、时时通信(websocket)、离线存储(applicationCache)、开启多线程(web worker)...
查看>>
常用正则表达式
查看>>
队列的基本使用方法
查看>>
解题:USACO18FEB Taming the Herd
查看>>
ACM-括号匹配问题
查看>>
使用Python中的urlparse、urllib抓取和解析网页(一)(转)
查看>>
Linux_屏蔽360、scanv、QQ管家等IP扫描
查看>>
LeetCode 538. Convert BST to Greater Tree
查看>>
@JoinColumn
查看>>
22_传智播客iOS视频教程_类的定义
查看>>
HDU 1856
查看>>