复习:我们学到哪里了 5 分钟
delete (1) of [questions v]:下面的全部往上移。insert [q] at (2) of [questions v]把后面的往下推。replace item (2) of [questions v] with [q]在原位置替换。
快问快答
你的问答游戏问蜘蛛有几条腿。一个同学打了 eight,却被判答错。为什么?
看答案
代码只把答案和 8 比较,别的都不管。答对的方法不只一种,今天就是要让你的问答游戏知道这一点。
今天的主题 3 分钟
<[accepted v] contains [8]?>:一次查遍整个列表- 用它来说“是”:这个答案是正确答案之一吗?
- 用它来说“不”:这个答案是我们不允许的吗?
- 让两个列表步调一致,每个列表的第 N 行是一对
学习目标
学完这一课,你会:
- 用一个积木,拿一个值和列表的每一行比较。
- 让一道题接受好几个不同的正确答案。
- 说出 contains 能做到、普通的等号做不到的事。
学一学:不用循环也能查找 28 分钟
1 · 名单
先从最简单的开始。问答晚会有一份玩家名单,只有名单上的人才能参加:
<[players v] contains (answer)?>
- 建一个叫
players的列表,放进四个名字。 - 加上
ask [What is your name?] and wait。 - 把
<[players v] contains ()?>拖进一个if / else。 - 把
(answer)放进它的空格。 - 在一半里欢迎他们,在另一半里请他们离开。
怎么用: 点绿旗,再点一下舞台。打 Mia 就能进去,打别的名字会被请走。
2 · 答对的方法不只一种
现在来看真正的用法。“How many legs has a spider?”(蜘蛛有几条腿?)只有一个答案,却有好几种写法。把每一种可以接受的写法都放进一个列表:
when flag clicked
delete all of [accepted v]
add [8] to [accepted v]
add [eight] to [accepted v]
add [viii] to [accepted v]
ask [How many legs has a spider?] and wait
if <[accepted v] contains (answer)?> then
say [Correct!] for (2) seconds
else
say [Not quite.] for (2) seconds
end
怎么用: 点绿旗,看四个答案依次和同一个列表比较。
3 · 同一个积木,用来说“不”
用 <not <>> 把它反过来,同一种列表就能把东西挡在外面。一个“不算真正答案的回答”列表:
if <not <[banned v] contains (answer)?>> then
say [Now that is an answer.] for (2) seconds
else
say [Have a proper go.] for (2) seconds
end
4 · 两个列表,一个行号
你的问答游戏已经把题目放在一个列表里了。把答案按同样的顺序放进第二个列表,每个列表的第 3 行就是一对:
(item (3) of [questions v])
(item (3) of [answers v])
一个循环就是这样问完并批改每一道题的——同一个 n 伸进两个盒子。
基础任务:一个公平打分的问答游戏 24 分钟
打开你的问答游戏。你要把一个等号换成一个 contains。
第 1 步:accepted 列表
- 建一个叫
accepted的列表。 - 在绿旗下面先清空它,然后把第一道题答案的每一种写法都加进去。
- 把大写开头的写法和明显的同义词也加进去。
第 2 步:用它来打分
- 找到给答案打分的那个 if。
- 把
<() = ()>从六边形里拖出来,扔掉。 - 换成
<[accepted v] contains (answer)?>。
第 3 步:答错时帮帮他们
- 在答错的那一半里,用
join把他们打的内容说回给他们听。 - 然后用
(item (1) of [accepted v])显示一个正确答案。
第 4 步:好好测试
- 用小写字母打答案。应该通过。
- 用大写字母打答案。应该通过。
- 打一个你加进去的同义词。应该通过。
- 打一个错的。应该不通过,而且告诉你正确答案。
when flag clicked
set [score v] to (0)
delete all of [accepted v]
add [yellow] to [accepted v]
add [gold] to [accepted v]
ask [What colour is a banana?] and wait
if <[accepted v] contains (answer)?> then
change [score v] by (1)
say [Correct!] for (2) seconds
else
say (join [No. ] (join (answer) [ is not one of these:])) for (2.5) seconds
say (item (1) of [accepted v]) for (1.5) seconds
end
say (join [Score: ] (score)) for (3) seconds
怎么用: 点绿旗,再点一下舞台。打 yellow 会被判答对,打别的就能看到答错的分支。
全部都判错,连正确答案也是。
检查的时候列表是空的。add 积木下面有一个 delete all of [accepted v],又把它们全删掉了——它应该放在上面。
全部都判对。
六边形是空的,而空的六边形算作 false——所以检查一下,你是不是不小心把 contains 放进了 else 那一半。
后面带空格的 “yellow ” 被判错。
没错——这很烦人。列表里的一行必须和整个答案一模一样。看看加分任务,就知道该怎么办。
accepted 框把答案显示给玩家看了。
测试完以后,在舞台上取消它的勾,不然你的问答游戏就把答案泄露了。
不许放弃
有些回答不算答案。用一个列表把它们挡在外面。
- 一个叫
banned的列表,里面至少有四个不算答案的回答。 - 打了其中一个,不算答了一次。
- 其他的回答都算真正的尝试。
怎么用: 点绿旗,看三次尝试和 banned 列表比较。
Teacher note
The interesting bit is the <not <>>. Pupils often build it the other way round and end up only accepting the banned words — which is worth letting them run once before you say anything.
选择题
显示四个选项——从列表里读出来,而不是打在代码里。
- 一个叫
choices的列表,里面有 A、B、C 和 D。 - 主持人用一个循环把四个选项都读出来。
- 正确的选项用它的行号来指定,而不是用它的文字。
怎么用: 点绿旗,看四个选项被读出来,然后说出答案。
Teacher reveal
set [n v] to (0)
repeat (length of [choices v])
change [n v] by (1)
say (item (n) of [choices v]) for (1.1) seconds
end
say (join [The answer is ] (item (2) of [choices v])) for (3) seconds
Same walking loop as Lesson 3-5, on a different list. The point to draw out is that the options and the code are now separate things — a pupil can write a whole new question without opening the scripts.
步调一致的两个列表
用一个循环和两个列表,给问答游戏的每道题打分。
questions和answers,长度一样,顺序一样。- 一个计数器同时伸进两个列表。
- 主持人可以读出任何一道题和它的答案。
怎么用: 点绿旗。先显示每个列表的第 3 行,然后五对题目和答案依次过去。
Teacher reveal — the pairing is a promise, not a rule
repeat (length of [questions v])
change [n v] by (1)
say (join (item (n) of [questions v]) (join [ = ] (item (n) of [answers v]))) for (1.3) seconds
end
Nothing in Scratch makes the two lists match up — the pupil keeps that promise by hand, and the moment they add to one and not the other it breaks silently. Ask them to break it on purpose and watch. The tidy answer is Lesson 3-14’s My Block: one place that adds to both.
等号做不到的事
自己找出列表到底帮了你什么。四个人打一个答案;数一数普通的 = 判对了几个,contains 又判对了几个。
- 四次尝试:正确答案、大写的正确答案、后面多一个空格的正确答案,还有一个同义词。
- 一个计数器给
<() = ()>,一个给 contains。 - 两个数不一样,而且你能准确说出为什么。
怎么用: 点绿旗,最后看两个计数器。它们不一样。
Teacher reveal
if <(tried) = [honey]> then
change [strict v] by (1)
end
if <[accepted v] contains (tried)?> then
change [kind v] by (1)
end
Run it in front of the class and ask them to predict the two numbers first. Nearly everybody predicts 1 for the equals, because they expect capitals to break it. The answer is 2, and being wrong about that in public is the fastest way to remember what Scratch’s equals actually does.
总结 5 分钟
<[accepted v] contains (answer)?>查遍每一行,报出 true 或 false。- 一个积木,就能做本来要用计数器、循环和 if 才能做的事。
- 用
<not <>>包起来,同一个积木就能把东西挡在外面,而不是放进来。 - 步调一致的两个列表共用一个行号——让它们保持一致的是你,不是 Scratch。
- Scratch 的
<() = ()>本来就不管大小写。需要列表来解决的,是空格和同义词。
- 包含(Contains)
- 一个值是列表中的某一行时,就是 true。
- 查找(Search)
- 在列表里找某样东西。
- 可以接受的答案
- 一个列表,里面是所有应该算答对的回答。
- 步调一致(In step)
- 两个列表的行号对得上。
交作业
保存你的问答游戏。下一课是这个单元的最后一课:一轮知道自己什么时候结束的游戏、三条生命值、一张成绩卡,还有一个点绿旗也不会消失的最高分。