Count a value · 统计某个值
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
This one builds on a lesson you haven't finished yet. Do that lesson first: · 此任务基于您尚未完成的课程。请先完成以下课程:
Python Basics · Python 基础 · Functions · 函数 →
English
Count a value
Count how often one value appears in a list.
Loop over the list and compare each item with target. It is the counter pattern again, with the condition item == target, and it works for words as well as numbers.
中文
统计某个值
统计某个值在列表中出现的次数。
遍历列表,把每个元素和 target 比较。这还是计数的方法,只是条件变成了 item == target。它对单词和数字都适用。
Write count_value(items, target) that returns how many times target appears in items, without using .count(). · 编写 count_value(items, target),返回 target 在 items 中出现了几次,不能使用 .count()。
Click Run to see the output here. · 点击“运行”查看此处输出。