Night 5: Is your pet hungry?
You already finished this night, in Python or pseudocode. It counts for both. Practise again if you like.
Tonight: help your pet decide
- Is your pet hungry or happy? It depends on its energy.
- You will learn one thing:
IFandELSE.
Words for tonight
| Word | Meaning |
|---|---|
| if | only when something is true |
| else | in all other cases |
| condition | a question with the answer TRUE or FALSE, like Energy < 5 |
IF asks a question
IFasks a question.THENgoes at the end of the same line.- When the answer is
TRUE, the lines below run. ENDIFcloses theIF. EveryIFneeds its ownENDIF.
DECLARE Energy : INTEGER
Energy ← 2
IF Energy < 5 THEN
OUTPUT "I am hungry"
ENDIF
OUTPUT "Bye!"
OUTPUT "Bye!"comes afterENDIF. It always runs.- Move the lines inside the
IFto the right (indent them). It helps people read your code.
ELSE is the other way
- The lines after
ELSErun when the answer isFALSE. - Only one of the two parts runs, never both.
DECLARE Energy : INTEGER
Energy ← 8
IF Energy < 5 THEN
OUTPUT "I am hungry"
ELSE
OUTPUT "I am happy"
ENDIF

Question signs
| Sign | Question | TRUE example |
|---|---|---|
< |
less than? | 3 < 5 |
> |
more than? | 9 > 5 |
= |
the same? | "moon" = "moon" |
<> |
different? | "sun" <> "moon" |
<= |
less than or the same? | 5 <= 5 |
← or =?
- The arrow
←puts a value in a box:Word ← "moon". - After
IF, one=asks a question:IF Word = "moon" THEN - Text must match exactly.
"Moon"and"moon"are different.
Now you try
- Do the tasks below. Press Run, then Check answer.
- Check that every
IFhasTHENon its line and anENDIFat the end.
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
Read the energy with INPUT Energy. If energy is less than 5, output I am hungry. Otherwise, output I am happy.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
Your pet guards a door. Read a word with INPUT Word. If the word is moon, output Come in!. Otherwise, output Go away!.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 3/5 · 200 pièces + un petit bonus lors de la première réussite aujourd'hui
Read the energy with INPUT Energy. Below 3: output Very hungry. Below 7: output OK. Otherwise: output Full of energy. Pseudocode has no ELSEIF, so put a second IF inside the ELSE.
Cliquez sur Exécuter pour voir le résultat ici.