12 — Random: dice and picks
A game needs a surprise. Rolling a die, or picking one of several things, is a single sentence. There is nothing to load first and nothing to switch on.
Steps
Type this into the writing box:
run it →set die to random number from 1 to 6 show dieRun it a few times. Any number from 1 to 6 comes out. Korean is
주사위는 1부터 6까지 무작위 숫자.Picking one of the things you name:
run it →set color to pick from red or green or blue show colorKorean is
색은 빨강 또는 초록 또는 파랑 중에서 골라.Mix it with what you already know and it is a game:
run it →set answer to random number from 1 to 3 ask number guess Pick 1, 2 or 3 if guess equals answer show Correct! else show Not this time show answer endRoll several times and keep them:
run it →set rolls to list of repeat 3 times set one to random number from 1 to 6 append one to rolls end show rolls(Skip this for now.) In beginner syntax the same jobs have helper names. You load them with
use randomand then writerandom_number(1, 6). Written as sentences, that line is not needed.
Try it yourself
Roll the die twice and show both:
set first to random number from 1 to 6
set second to random number from 1 to 6
show first
show second
run it →What you learned
random number from 1 to 6/1부터 6까지 무작위 숫자rolls a die.pick from red or green or blue/... 중에서 골라picks one of several.- Written as sentences, nothing has to be loaded first.
- Chance mixes straight into conditions, loops and lists — mix them and you have a game.
- Beginner syntax calls the same jobs by name, after
use random. That comes later, in 61 — Modules.