Making decisions · Prendre des décisions
Making a choice
ifruns some code only when a condition is true.- The condition is followed by a colon
:. - The lines that belong to the
ifare indented (4 spaces).
Faire un choix
ifexécute du code uniquement lorsque une condition est vraie.- La condition est suivie d'un deux-points
:. - Les lignes qui appartiennent au
ifsont indentées (4 espaces).
Comparisons
- A condition is usually a comparison that is True or False.
==equal,!=not equal,<><=>=.- Note:
==tests equality; a single=stores a value.
Comparaisons
- Une condition est généralement une comparaison qui est True ou False.
==égal,!=non égal,<><=>=.- Note :
==test l'égalité ; un simple=stocke une valeur.
temperature = 30
if temperature > 25:
print("It is hot.")
if / elif / else
elif("else if") checks another condition if the first was false.elseruns when none of the conditions were true.- Python checks them in order and stops at the first true one.
if / elif / else
elif("else if") vérifie une autre condition si la première était fausse.elseexécute lorsque aucune des conditions n'était vraie.- Python les vérifie dans l'ordre et s'arrête au premier vrai.
hour = 14
if hour < 12:
print("Morning")
elif hour < 18:
print("Afternoon")
else:
print("Evening")
The remainder
a % bis what is left over whenais divided byb.year % 4 == 0means the year divides by 4 exactly — nothing left over.8 % 3is2, because 8 is two 3s with 2 left over.
Le reste
a % best ce qui est rester quandaest divisé parb.year % 4 == 0signifie que l'année se divise par 4 exactement — rien de reste.8 % 3est2, car 8 contient deux 3 avec 2 de reste.
Combine conditions
andis true only when both sides are true.oris true when at least one side is true.notflips true to false and false to true.
Combiner des conditions
andest vrai uniquement quand les deux côtés sont vrais.orest vrai quand au moins un côté est vrai.notinverse vrai en faux et faux en vrai.
day = "Sat"
if day == "Sat" or day == "Sun":
print("Weekend!")
In Cambridge pseudocode
- Python
if/elif/elsematches the exam'sIF … ELSE … ENDIFandCASE.
En pseudocode Cambridge
- Python
if/elif/elsecorrespond auxIF … ELSE … ENDIFetCASEde l'examen.
IF Score >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
CASE OF Grade
"A": OUTPUT "Top"
"B": OUTPUT "Good"
OTHERWISE OUTPUT "Keep trying"
ENDCASE
Common mistakes
- Compare with
==; a single=assigns. - Write
elif, notelse if, and end eachif/elif/elseline with:. - The indented block below
ifis what runs when the condition is true.
Erreurs courantes
- Comparez avec
==; un simple=affecte. - Écrivez
elif, paselse if, et terminez chaque ligneif/elif/elsepar:. - Le bloc indenté sous
ifest ce qui s'exécute quand la condition est vraie.
Now you try
- When you Run, type the input in the box under the code and press Enter.
- The checks type the input for you. Read it, then decide what to print.
- Match the output text exactly.
À vous maintenant
- Lorsque vous Exécutez, tapez l'entrée dans la boîte sous le code et appuyez sur Entrée.
- Les vérifications tapent l'entrée pour vous. Lisez-la, puis décidez quoi imprimer.
- Correspondez au texte de sortie exactement.
if / elif / else
Conditions are checked top-down; only the first true branch runs. · Les conditions sont vérifiées de haut en bas ; seule la branche vraie en premier s'exécute.
Read a score with int(input()). Print Pass if it is 50 or more, otherwise print Fail. For input 72, print Pass. · Lisez un score avec int(input()). Affichez Pass s'il est 50 ou plus, sinon affichez Fail. Pour l'entrée 72, affichez Pass.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Read a score. Print A if it is 80+, B if it is 60+, otherwise C. Use if / elif / else. For input 75, print B. · Lisez un score. Imprimez A s'il est 80+, B s'il est 60+, sinon C. Utilisez if / elif / else. Pour l'entrée 75, imprimez B.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Read an age. Print Yes if the age is from 13 to · à 19 (a teenager), otherwise No. Use and. For input 16, print Yes. · Lisez un âge. Affichez Yes si l'âge est compris entre 13 et 19 (un adolescent), sinon No. Utilisez and. Pour l'entrée 16, affichez Yes.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Read a year with int(input()). A year divides by 4 when the remainder is 0: year % 4 == 0. Print Leap if it divides by 4, otherwise Common. For the input 2024, print Leap. · Lisez une année avec int(input()). Une année est divisible par 4 quand le reste est 0 : year % 4 == 0. Imprimez Leap si elle est divisible par 4, sinon imprimez Common. Pour l'entrée 2024, imprimez Leap.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Dividing by 4 is not the whole rule. A year that also divides by 100 is not · non a leap year, unless it divides by 400 too. 1900 is the trap: it divides by 4 and by 100, so print Common. Print Leap or · ou Common. · Se diviser par 4 n'est pas toute la règle. Une année qui se divise aussi par 100 n'est pas une année bissextile, sauf si elle se divise aussi par 400. 1900 est le piège : elle se divise par 4 et par 100, donc affichez Common. Affichez Leap ou Common.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.