FizzBuzz
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
FizzBuzz
The classic. Walk the numbers 1 to n and, for each, decide what string to add to a list:
- divisible by both 3 and 5 →
FizzBuzz - divisible by 3 →
Fizz - divisible by 5 →
Buzz - otherwise → the number itself, as a string
Check the "both" case first, or a multiple of 15 will be caught by the 3 test alone.
中文
FizzBuzz
经典题目。遍历数字 1 到 n,为每个数字决定往列表里加入哪个字符串:
- 同时能被 3 和 5 整除 →
FizzBuzz - 能被 3 整除 →
Fizz - 能被 5 整除 →
Buzz - 其他 → 数字本身的字符串形式
要先检查「两者都能」的情况,否则 15 的倍数会被单独的「能被 3 整除」判断先捕获。
Write fizzbuzz(n) returning a list for the numbers 1..n: Fizz if divisible by 3, Buzz if by 5, FizzBuzz if by both, otherwise the number as a string. · 编写 fizzbuzz(n),返回 1 到 n 这些数字的列表:能被 3 整除写 Fizz,能被 5 整除写 Buzz,两者都能整除写 FizzBuzz,否则写该数字的字符串形式。
Click Run to see the output here. · 点击“运行”查看此处输出。