Selection — IF and CASE · Sélection — IF et CASE
Making a decision
- Selection means choosing what to do, based on a test.
- An
ifruns its block only when the test is true. - The test gives a Boolean:
TrueorFalse.
Prendre une décision
- La sélection consiste à choisir quoi faire, selon un test.
- Un
ifexécute son bloc uniquement lorsque le test est vrai. - Le test donne un booléen :
TrueouFalse.
age = 20
if age >= 18:
print("You may vote")
else: the other path
elseruns when theiftest is false.- Exactly one of the two blocks runs.
else : l'autre chemin
elses'exécute lorsque le testifest faux.- Exactement l'un des deux blocs s'exécute.
mark = 45
if mark >= 50:
print("Pass")
else:
print("Fail")
elif: many paths
- Use
elifto test more conditions in order. - Python checks each test top to bottom and stops at the first true one.
elif : plusieurs chemins
- Utilisez
elifpour tester plus de conditions dans l'ordre. - Python vérifie chaque test de haut en bas et s'arrête au premier vrai.
mark = 72
if mark >= 70:
print("A")
elif mark >= 50:
print("B")
else:
print("C")
if / elif / else — which runs? · if / elif / else — lequel s'exécute ?
Tests run top-down; the first · premier true one runs, the rest are skipped. · Les tests s'exécutent de haut en bas ; le premier vrai s'exécute, les autres sont ignorés.
Comparison operators
==means equal to (the exam writes=).!=means not equal to (the exam writes<>).- Also use
<,>,<=,>=.
Opérateurs de comparaison
==signifie égal à (l'examen écrit=).!=signifie différent de (l'examen écrit<>).- Utilisez aussi
<,>,<=,>=.
print(5 == 5) # True
print(5 != 3) # True
print(3 <= 2) # False
In the exam: IF and CASE
- Pseudocode uses
IF … THEN … ELSE … ENDIF, orCASEfor many fixed choices.
En examen : IF et CASE
- La pseudocode utilise
IF … THEN … ELSE … ENDIF, ouCASEpour de nombreux choix fixes.
IF Mark >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Common mistakes
- Compare with
==, not=. - Cover every case, including the
else. - Indent the block that belongs to each branch.
Erreurs courantes
- Comparez avec
==, pas=. - Couvrez tous les cas, y compris le
else. - Indentez le bloc appartenant à chaque branche.
Now you try
- Read the input, then choose what to print with
if/elif/else. - Press Check answer to test your code.
À vous maintenant
- Lisez l'entrée, puis choisissez quoi afficher avec
if/elif/else. - Appuyez sur Vérifier la réponse pour tester votre code.
Read a whole number, then print even if it divides by 2 with no remainder, otherwise print odd. For input 4, print even. · Lisez un nombre entier, puis affichez even si c'est divisible par 2 sans reste, sinon affichez odd. Pour l'entrée 4, affichez even.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Read a mark (0–100). Print A for 70 or more, B for 50–69, and C for anything less. For input 65, print B. · Lisez une note (0–100). Affichez A pour 70 ou plus, B pour 50–69, et C pour tout le reste. Pour l'entrée 65, affichez B.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Two numbers a and · et b are set for you. Use an if to store the larger · plus grande one in biggest. · Deux nombres a et b sont définis pour vous. Utilisez un if pour stocker le plus grand dans biggest.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.