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



|
4月前
|
JSON C语言 C++
【Python 基础教程 26】Python3标准库全面入门教程：一步步带你深入理解与应用
【Python 基础教程 26】Python3标准库全面入门教程：一步步带你深入理解与应用
92 1
|
27天前
|
XML 编解码 数据可视化
MoJoCo 入门教程（六）Python LQR 教程
MoJoCo 入门教程（六）Python LQR 教程
36 2
|
27天前
|
XML 传感器 数据可视化
MuJoCo 入门教程（三）Python 绑定
MuJoCo 入门教程（三）Python 绑定
43 4
|
27天前
|

MuJoCo 入门教程（五）Python 绑定（下）
MuJoCo 入门教程（五）Python 绑定（下）
21 2
|
27天前
|
XML 数据可视化 安全
MuJoCo 入门教程（五）Python 绑定
MuJoCo 入门教程（五）Python 绑定（上）
39 1
|
13天前
|

Python强大的信号库-blinker 入门教程
Python强大的信号库-blinker 入门教程
8 0
|
13天前
|
Python
Python中的装饰器入门教程
【8月更文挑战第28天】在Python的世界里，装饰器是那些让代码更加简洁、功能更强大的神奇工具。它们就像是给函数穿上了一件华丽的外衣，让函数在执行前后拥有更多的可能性。本文将带你了解装饰器的概念、用法和如何自己动手编写一个装饰器。准备好了吗？让我们一起揭开装饰器的神秘面纱！
5 0
|
2月前
|

python小白快速入门教程
Python是一种解释型、面向对象、动态数据类型的高级编程语言。
37 1
|
3月前
|

151 3
|
3月前
|

55 2

DDNS