Sum a list · 求列表之和
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
Sum a list
Add up every number in a list and return the total. Start a running total at 0, then loop over the list and add each value to it.
This pattern — a running total that grows inside a loop — comes up everywhere in programming.
中文
求列表之和
把列表中的每个数字加起来并返回总和。先把累加值设为 0,然后遍历列表,把每个值加上去。
这种模式 —— 在循环里不断增长的累加值 —— 在编程中随处可见。
Write sum_list(xs) that returns the total of all numbers in the list xs. An empty list sums to 0. · 编写 sum_list(xs),返回列表 xs 中所有数字的总和。空列表的总和为 0。
Click Run to see the output here. · 点击“运行”查看此处输出。