Night 5: Is your pet hungry?
Cette page nécessite un navigateur récent (avec support SharedArrayBuffer). Veuillez mettre à jour Chrome, Edge, Firefox ou Safari vers la dernière version.
English
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 |
| indent | 4 spaces at the start of a line |
English
if asks a question
ifasks a question. The answer isTrueorFalse.- When it is
True, Python runs the indented lines below. - The line with
ifends with a colon:.
energy = 2
if energy < 5:
print("I am hungry")
print("Bye!")
print("Bye!")has no indent. It always runs.
English
else is the other way
else:runs when the question isFalse.- Python runs one of the two blocks, never both.
energy = 8
if energy < 5:
print("I am hungry")
else:
print("I am happy")
English
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 |
English
= or ==?
- One
=puts a value in a box:word = "moon". - Two
==ask a question:if word == "moon": - Text must match exactly.
"Moon"and"moon"are different.
English
Now you try
- Do the tasks below. Press Run, then Check answer.
- Watch the indent: 4 spaces after every line that ends with
:.
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
Read the energy with int(input()). If energy is less than 5, print I am hungry. Otherwise, print 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(). If the word is moon, print Come in!. Otherwise, print 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 int(input()). Below 3: print Very hungry. Below 7: print OK. Otherwise: print Full of energy. Use if, elif et else.
Cliquez sur Exécuter pour voir le résultat ici.