main.py
Costs 1 credit
加载编辑器中...
输出 Output执行成功
[错误提示] 检测到中文全角符号(如 ( ) , : ;),Python 语法可能无法解析。 请改成英文半角符号:( ) , : ;
Terminal (Simulated)Install commands are simulated only, no real network requests
$
Step 1: 第一个生成器
生成器函数用 yield 按需产出数据,而不是一次性全部返回。
底层逻辑:
每次遇到
这就是惰性求值的核心机制。
每次遇到
yield 会“暂停并返回一个值”,下次迭代从暂停点继续执行。这就是惰性求值的核心机制。
你的任务:
- 创建生成器函数
count_up_to(n) - 从 1 yield 到 n
- 用 for 循环遍历
count_up_to(5)并输出每个值
Self-Check List
- 创建基本生成器
Transfer Template
def count_up_to(n):
for i in range(1, n + 1):After completing this step, you should be able to independently explain and reproduce this concept, then apply it to similar problems.