Counting loops · Boucles de comptage
Repeat with for
- A
forloop repeats code a fixed number of times. - The body of the loop is indented, like an
if. - A loop variable holds a new value on each pass.
Répéter avec for
- Une boucle
forrépète du code un nombre fixe de fois. - Le corps de la boucle est indenté, comme un
if. - Une variable de boucle contient une nouvelle valeur à chaque passage.
Counting with range
range(5)gives the numbers0, 1, 2, 3, 4(stops before 5).range(1, 6)gives1, 2, 3, 4, 5(start included, stop not).range(0, 10, 2)counts in steps of 2.
Compter avec range
range(5)donne les nombres0, 1, 2, 3, 4(s'arrête avant 5).range(1, 6)donne1, 2, 3, 4, 5(début inclus, fin non incluse).range(0, 10, 2)compte par pas de 2.
for i in range(5):
print(i)
Build a total (accumulator)
- An accumulator is a variable that grows inside a loop.
- Start it at
0before the loop. - Add to it on each pass to build a running total.
Construire un total (accumulateur)
- Un accumulateur est une variable qui grandit à l'intérieur d'une boucle.
- Commencez-le à
0avant la boucle. - Ajoutez-y à chaque passage pour construire un total cumulé.
total = 0
for n in range(1, 6):
total = total + n
print(total)
In Cambridge pseudocode
- A Python
for … rangeloop is the exam's count-controlledFOR … NEXT.
En pseudocode Cambridge
- Une boucle Python
for … rangeest le compteur contrôléFOR … NEXTde l'examen.
Total ← 0
FOR I ← 1 TO 5
Total ← Total + I
NEXT I
OUTPUT Total
Words and a number on one line
- A comma inside
printputs a space between the parts. print("7 x", 1, "=", 7 * 1)prints7 x 1 = 7.+cannot join a number to text. Use the comma, or turn the number into text withstr.
Mots et un nombre sur une même ligne
- Une virgule à l'intérieur de
printmet un espace entre les parties. print("7 x", 1, "=", 7 * 1)affiche7 x 1 = 7.+ne peut pas joindre un nombre à du texte. Utilisez la virgule, ou convertissez le nombre en texte avecstr.
Common mistakes
range(n)counts0ton - 1, not1ton.range(a, b)stops beforeb.- The loop body must be indented under the
forline.
Erreurs courantes
range(n)compte de0àn - 1, pas de1àn.range(a, b)s'arrête avantb.- Le corps de la boucle doit être indenté sous la ligne
for.
Now you try
- Use a
forloop withrange(...)in each task. - Press Check answer to test your code.
À vous maintenant
- Utilisez une boucle
foravecrange(...)dans chaque tâche. - Appuyez sur Vérifier la réponse pour tester votre code.
A counting loop, pass by pass · Une boucle de comptage, étape par étape
A for loop repeats its body; watch the counter and the running total · total cumulé. · Une boucle for répète son corps ; surveillez le compteur et le total accumulé.
Use a for loop to print the numbers 1 to · à 5, each on its own line. · Utilisez une boucle for pour afficher les nombres 1 à 5, chacun sur sa propre ligne.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Use a loop and an accumulator to add up every whole number from 1 to · à 100. Store the total in total. · Utilisez une boucle et un accumulateur pour additionner tous les nombres entiers de 1 à 100. Stockez le total dans total.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Multiply together every whole number from 1 to · à 5 (that is 1×2×3×4×5). Start product at 1 and store the result there. · Multipliez ensemble tous les nombres entiers de 1 à 5 (c'est-à-dire 1×2×3×4×5). Commencez product à 1 et stockez le résultat là.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Print exactly one line: 7 x 1 = 7. Finish the print that is already started. A comma between the parts prints them with spaces: print("7 x", 1, "=", 7 * 1). · Affichez exactement une ligne : 7 x 1 = 7. Terminez la print qui est déjà commencée. Une virgule entre les parties les imprime avec des espaces : print("7 x", 1, "=", 7 * 1).
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Use a for loop to print the first five lines of the 7 times table: 7 x 1 = 7, 7 x 2 = 14, up to 7 x 5 = 35. The print from the last task is already inside the loop — change the 1 into the loop variable. · Utilisez une boucle for pour afficher les cinq premières lignes de la table de 7 : 7 x 1 = 7, 7 x 2 = 14, jusqu'à 7 x 5 = 35. Le print de la tâche précédente est déjà dans la boucle — remplacez le 1 par la variable de boucle.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.