Putting it all together · 把所有内容综合起来
Putting it all together
- You now know variables, input,
if, loops, lists, functions, and dictionaries. - A real program combines these small pieces into one useful tool.
- In this lesson you build a tiny class-scores analyzer, one function at a time.
把所有内容综合起来
- 你现在已经会变量、输入、
if、循环、列表、函数和字典了。 - 一个真正的程序会把这些小部件组合成一个有用的工具。
- 这节课你将一次写一个函数,搭出一个小小的班级成绩分析器。
The plan
- We have a list of test scores, like
[70, 90, 50, 80]. - We want three answers: the average, how many passed, and a short summary.
- We will write one small function for each job, then they work together.
计划
- 我们有一个成绩列表,比如
[70, 90, 50, 80]。 - 我们想要三个答案:平均分、有多少人及格,以及一段简短的总结。
- 我们会为每件事写一个小函数,然后让它们协同工作。
The analyzer calls each helper · 分析器逐个调用每个小函数
A big program calls small functions in turn, each returning one answer. · 大程序依次调用小函数,每个返回一个答案。
Handy built-ins for lists
- Python gives you ready-made helpers that work on a list of numbers.
len(x)counts the items,sum(x)adds them,max(x)/min(x)find the largest / smallest.- Use them instead of writing a loop every time.
处理列表的便捷内置函数
- Python 提供了一些现成的小助手,可以直接用在数字列表上。
len(x)数有多少个,sum(x)求和,max(x)/min(x)找出最大 / 最小值。- 用它们,而不必每次都自己写一个循环。
scores = [70, 90, 50, 80]
print(len(scores)) # 4
print(sum(scores)) # 290
print(max(scores)) # 90
print(min(scores)) # 50
A pattern you will reuse: count items that pass a test
- A very common job: loop a list, and count the items that pass some test.
- Start a counter at
0, and add1each time theifis true. - Here we count the long words; soon you will count the passing scores.
一个你会反复用到的套路:统计通过测试的元素
- 一个很常见的任务:遍历一个列表,统计有多少元素通过某个测试。
- 让计数器从
0开始,每当if为真时就加1。 - 这里我们统计长单词;很快你就会去统计及格的成绩。
def how_many_long_words(words, min_length):
count = 0
for w in words:
if len(w) >= min_length:
count = count + 1
return count
print(how_many_long_words(["hi", "hello", "hey", "howdy"], 4)) # 2
Common mistakes
- Build in small pieces and test each one before adding the next.
- Read the whole task first, then plan before you code.
常见错误
- 小块构建,每写好一块就测试,再加下一块。
- 先读完整个题目,规划好再写代码。
Now you try
- Build the analyzer in three steps:
average,count_passing, thenstats. - Each function is small. Together they turn a list of scores into real answers.
- Press Check answer after each one.
现在轮到你
- 分三步搭出分析器:
average、count_passing,然后stats。 - 每个函数都很小。它们合在一起,就能把一个成绩列表变成真正的答案。
- 每完成一个就按检查答案。
Write average(scores) that returns the average (mean) of a list of numbers: the total divided by how many. If the list is empty · 空的, return 0 (so it does not crash). Example: average([2, 4]) is 3.0. · 编写 average(scores),返回一个数字列表的平均值(均值):总和除以个数。如果列表是空的,返回 0(这样就不会崩溃)。例如:average([2, 4]) 是 3.0。
Click Run to see the output here. · 点击“运行”查看此处输出。
Write count_passing(scores, pass_mark) that returns how many scores are greater than or equal to pass_mark. Loop the list and count. Example: count_passing([40, 50, 60], 50) is 2. · 编写 count_passing(scores, pass_mark),返回有多少个成绩大于或等于 pass_mark。遍历列表并计数。例如:count_passing([40, 50, 60], 50) 是 2。
Click Run to see the output here. · 点击“运行”查看此处输出。
Write stats(scores) (assume at least one score) that returns a dictionary with three keys: "highest", "lowest", and "average". Example: stats([70, 90, 50, 80]) returns · 返回值 {"highest": 90, "lowest": 50, "average": 72.5}. · 编写 stats(scores)(假设至少有一个成绩),返回一个带三个键的字典:"highest"、"lowest" 和 "average"。例如:stats([70, 90, 50, 80]) 返回 {"highest": 90, "lowest": 50, "average": 72.5}。
Click Run to see the output here. · 点击“运行”查看此处输出。
Write grade_counts(scores) that returns · 返回值 a dictionary counting grades: A for 80 and above, B for 60 and above, C for the rest. Always include all three keys: grade_counts([85, 72, 90, 40]) is {"A": 2, "B": 1, "C": 1}. · 编写 grade_counts(scores),返回一个统计等级的字典:80 分及以上是 A,60 分及以上是 B,其余是 C。三个键都要有:grade_counts([85, 72, 90, 40]) 是 {"A": 2, "B": 1, "C": 1}。
Click Run to see the output here. · 点击“运行”查看此处输出。