- 学一学28 分钟 · 列表、add、item (N) of、length of
- 基础任务24 分钟 · 一个自己走完列表的问答游戏
- 挑战 1–327 分钟 · 第六题、进度、最后一题
- 加分任务加分 · 按列表算出的得分
复习:自动售货机单元 5 分钟
([floor v] of ())和(() mod ())把钱数分成硬币。(pick random (1) to (6))让机器每次运行都不一样。- 变量是一个有名字的盒子,它只装一样东西。
快问快答
你的机器卖出了三份零食。它在把三个价格加起来的时候,把这三个价格放在哪里了?
看答案
它没有留着。它把每个价格加进 basket,然后就扔掉了。今天你会有一个盒子,能把三个都留住——还能再读出来。
今天的主题 3 分钟
- 列表:一个按顺序装很多东西的变量
add [thing] to [questions v](item (1) of [questions v]):还有为什么行号从 1 开始(length of [questions v]):里面有几个
学习目标
学完这一课,你会:
- 建一个列表,并把东西放进去。
- 按编号读出任何一行。
- 写一个循环,不管列表多长都能用。
学一学:装下一整套题目的盒子 28 分钟
1 · 为什么一个变量不够
你想做一个有五道题的问答游戏。用变量,就是 q1、q2、q3、q4、q5——五个盒子、五个名字,还有五份差不多一样的积木。
然后有人要第六题,你就得改代码。
列表是一个盒子,里面有很多行。五道题,一个名字,再加第六题一点也不费事。
2 · 建一个列表,再把它装满
- 到 Variables,点 Make a List(建立一个列表)。
- 给它取名
questions,选“适用于所有角色”。 - 舞台上出现一个高高的空盒子。那就是列表。
- 拖出
add [thing] to [questions v],在里面打一道题。 - 点一下这个积木。看盒子里出现一行。
when flag clicked
delete all of [questions v]
add [What colour is a banana?] to [questions v]
add [How many legs has a spider?] to [questions v]
add [What do bees make?] to [questions v]
add [Which planet do we live on?] to [questions v]
add [What is frozen water called?] to [questions v]
delete all 很重要——看下面的“小心”。怎么用: 点绿旗,看左边的盒子一行一行装满。
3 · 读出其中一行
每一行都有编号。在 Scratch 里,第一行是 1 号——不是 0 号。
(item (1) of [questions v])
(item (3) of [questions v])
4 · 里面有几个?
(length of [questions v])
把这两个积木合在一起,你不用知道列表有多长,也能拿到最后一行:
say (item (length of [questions v]) of [questions v])
怎么用: 点绿旗,看主持人读出第 1 项、第 3 项、一共几项,还有最后一行。
5 · 走完整个列表
只要一个计数器和一个重复就够了。计数器是 1、2、3……而 (item (n) of [questions v]) 跟着它一行一行往下走:
when flag clicked
set [n v] to (0)
repeat (length of [questions v])
change [n v] by (1)
say (item (n) of [questions v]) for (1.6) seconds
end
基础任务:一个自己走完列表的问答游戏 24 分钟
新建一个作品。选一个你喜欢的主持人角色和背景。
第 1 步:列表
- 建一个叫
questions的列表。 - 在绿旗下面,放上
delete all of [questions v]。 - 加五道你自己的题,每题一个积木。
- 点两次绿旗,检查是不是还是五行,而不是十行。
第 2 步:读出一题
- 加上
say (item (1) of [questions v]) for (2) seconds。 - 把 1 改成 4,再运行一次。
第 3 步:计数器
- 建一个叫
n的变量,在绿旗下面把它设为 0。 - 加上
repeat (length of [questions v])。 - 在它里面:先放
change [n v] by (1),再放一个用到(item (n) of [questions v])的 say。
第 4 步:给题目编号
- 每道题之前,先说
(join [Q] (n))。 - 检查舞台上的编号和盒子里的行号对得上。
第 5 步:证明它能变大
- 再加一个 add 积木,加上第六题。
- 运行它。不要碰循环。
- 现在从脚本上面删掉两道题,再运行一次。
when flag clicked
delete all of [questions v]
add [What colour is a banana?] to [questions v]
add [How many legs has a spider?] to [questions v]
add [What do bees make?] to [questions v]
set [n v] to (0)
repeat (length of [questions v])
change [n v] by (1)
say (join [Q] (n)) for (0.5) seconds
say (item (n) of [questions v]) for (1.6) seconds
end
say [That is the lot.] for (2) seconds
怎么用: 点绿旗,看它从第 1 行一直走到最后。
每次点绿旗,列表都会变长。
最上面少了 delete all of [questions v]。列表是绿旗唯一不会帮你重置的东西。
第一个气泡是空的,然后只问了四道题。
change [n v] by (1) 放在了 say 下面,而不是上面,所以第一轮它要的是第 0 项。
它把同一道题问了五次。
say 里放的是 (item (1) of [questions v]),而不是 (item (n) of [questions v])。
明明有五道题,它问了三道就停了。
repeat 里是打上去的数。把 (length of [questions v]) 拖进那个空格。
白送的第六题
证明你的问答游戏会自己变大。
- 用一个 add 积木加上第六题。
- 别的什么都不改。
- 游戏问了六道题,最后还会说出来。
怎么用: 点绿旗。六道题一道一道过去,而循环从来不知道有六道。
Teacher note
Worth doing out loud: ask the class to predict what happens, then add the question. The pupils who expected to edit the repeat are the ones who have not yet believed that (length of [questions v]) is live.
还剩几题
告诉玩家他们做到哪里了。
- 每道题之前:“Q3 of 5”(第 3 题,共 5 题)。
- 每道题之后:后面还有几题。
- 两个数都来自列表,而不是打上去的。
怎么用: 点绿旗,计数器往上走的时候,看那两个框。
Teacher reveal
say (join [Q] (join (n) (join [ of ] (length of [questions v])))) for (1) seconds
set [to go v] to ((length of [questions v]) - (n))
The subtract is the interesting block, not the joins. Ask what to go should be on the last question, and whether their code agrees.
宣布最后一题
给最后一道题来点隆重的。
- 只在最后一题之前,主持人说 “Last one!”(最后一题!)。
- 不管列表有三道题还是三十道题,它都能用。
- 最后,主持人再说一遍最后一题是什么。
怎么用: 点绿旗,注意在第五题之前出现的 “Last one!”。
Teacher reveal
if <(n) = (length of [questions v])> then
say [Last one!] for (1) seconds
end
say (item (n) of [questions v]) for (1.6) seconds
And at the end, (item (length of [questions v]) of [questions v]) — a length used as a row number, which is the first time the two blocks have been nested into each other. Pupils who hard-code a 5 in the if have solved today’s list and no other.
按列表算出的得分
再加一个装答案的列表,用它给每道题打分,最后说出得分——满分就是题目的数量。
- 第二个列表
answers,答案按同样的顺序排好。 - answers 的第 3 行,就是 questions 第 3 行的答案。
- 成绩卡显示 “2 out of 5”(5 题对了 2 题),是从列表算出来的。
怎么用: 点绿旗,看五道题都打完分,最后是成绩卡。
Teacher reveal
say (item (n) of [questions v]) for (1) seconds
if <(answer) = (item (n) of [answers v])> then
change [score v] by (1)
say [Right!] for (1) seconds
else
say (join [No - it is ] (item (n) of [answers v])) for (1) seconds
end
The demo above marks on the question number rather than on typing, so the class can watch both branches without anybody having to be wrong on purpose. The real thing uses (answer), as printed here.
总结 5 分钟
- 列表是一个盒子,按顺序一行一行地装很多东西。
add [thing] to [questions v]在最后加上一行。(item (3) of [questions v])读出一行。行号从 1 开始。(length of [questions v])数出有几行——所以循环可以处理任何长度的列表。- 点绿旗,列表不会清空。先用
delete all of [questions v]开始,不然它每次运行都会变长。
- 列表
- 一个装很多值的变量,从 1 开始编号。
- 项(Item)
- 列表里的一行。
- 索引(Index)
- 一行的编号。
- 长度(Length)
- 列表现在有几行。
交作业
保存你的问答游戏。下一课,列表不再只是一开始装好就不动了:答过的题会被删掉,新的题会被插到中间。