Add levels 04-07, code_contains test kind, StepValidator and reference-solution checks

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
denis
2026-10-16 12:50:19 +02:00
parent bb6bd8bc78
commit 4834979372
54 changed files with 1107 additions and 56 deletions
+4
View File
@@ -84,4 +84,8 @@ mission:
message:
fr: Il faut exactement trois lignes affichées, non vides.
en: Exactly three non-empty lines must be shown.
solution: |
print("Léa")
print("Lyon")
print("vélo")
reward_sound: success_small
+6
View File
@@ -133,4 +133,10 @@ mission:
en: "total must equal bill + tip."
- kind: stdout_equals
value: "57.5\n"
solution: |
bill = 50
rate = 0.15
tip = bill * rate
total = bill + tip
print(total)
reward_sound: success_small
+5
View File
@@ -143,4 +143,9 @@ mission:
message:
fr: Le programme doit afficher l'âge.
en: The program must print the age.
solution: |
current_year = 2025
year = int(input("Année de naissance ? "))
age = current_year - year
print(age)
reward_sound: success_small
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+30
View File
@@ -0,0 +1,30 @@
{
"en": {
"651b31ddd9a7a34e": {
"text": "Python follows priorities, like in maths: first **, then * / // %, then + and -. Parentheses come before everything.",
"voice": "en_US-ljspeech-high"
},
"9cd7a2aa73d6a258": {
"text": "Two very useful operators: // gives the whole quotient and % gives the remainder. For example, 17 // 5 is 3 and 17 % 5 is 2. There is also ** for powers.",
"voice": "en_US-ljspeech-high"
},
"f949912b9c04e712": {
"text": "Python calculates like a calculator: + adds, - subtracts, * multiplies, / divides. Careful: / always gives a decimal number.",
"voice": "en_US-ljspeech-high"
}
},
"fr": {
"633829630ac4c3d5": {
"text": "Python respecte les priorités, comme en maths : d'abord **, puis * / // %, puis + et -. Les parenthèses passent avant tout.",
"voice": "fr_FR-siwis-medium"
},
"baf63c4bda6a8543": {
"text": "Python calcule comme une calculatrice : + ajoute, - retire, * multiplie, / divise. Attention : / donne toujours un nombre à virgule.",
"voice": "fr_FR-siwis-medium"
},
"f2b33e527a6a90f1": {
"text": "Deux opérateurs très utiles : // donne le quotient entier et % donne le reste. Par exemple, 17 // 5 vaut 3 et 17 % 5 vaut 2. Il y a aussi ** pour les puissances.",
"voice": "fr_FR-siwis-medium"
}
}
}
+153
View File
@@ -0,0 +1,153 @@
id: 04-math
title:
fr: Calculer
en: Calculating
difficulty: 1
xp: 40
requires:
- 03-input
steps:
- type: explain
voice: true
text:
fr: "Python calcule comme une calculatrice : `+` ajoute, `-` retire, `*` multiplie, `/` divise. Attention : `/` donne toujours un nombre à virgule."
en: "Python calculates like a calculator: `+` adds, `-` subtracts, `*` multiplies, `/` divides. Careful: `/` always gives a decimal number."
- type: code
starter: |
price = 12
quantity = 3
print(price + quantity)
instruction:
fr: Ce code additionne le prix et la quantité. Corrige-le pour afficher le prix total.
en: This code adds the price and the quantity. Fix it to show the total price.
tests:
- kind: stdout_equals
value: "36\n"
message:
fr: Le prix total est le prix multiplié par la quantité.
en: The total price is the price multiplied by the quantity.
hints:
fr:
- Le signe de la multiplication est `*`.
en:
- The multiplication sign is `*`.
solution: |
price = 12
quantity = 3
print(price * quantity)
- type: explain
voice: true
text:
fr: "Deux opérateurs très utiles : `//` donne le quotient entier et `%` donne le reste. Par exemple, 17 // 5 vaut 3 et 17 % 5 vaut 2. Il y a aussi `**` pour les puissances."
en: "Two very useful operators: `//` gives the whole quotient and `%` gives the remainder. For example, 17 // 5 is 3 and 17 % 5 is 2. There is also `**` for powers."
- type: code
starter: |
minutes = 135
hours = 0
remaining = 0
print(hours, "h", remaining, "min")
instruction:
fr: "Calcule `hours` avec `//` et `remaining` avec `%` pour afficher 2 h 15 min."
en: "Compute `hours` with `//` and `remaining` with `%` to show 2 h 15 min."
tests:
- kind: var_equals
name: hours
value: 2
- kind: var_equals
name: remaining
value: 15
- kind: stdout_equals
value: "2 h 15 min\n"
hints:
fr:
- "Une heure, c'est 60 minutes : divise `minutes` par 60."
- "`hours = minutes // 60` puis `remaining = minutes % 60`."
en:
- "One hour is 60 minutes: divide `minutes` by 60."
- "`hours = minutes // 60` then `remaining = minutes % 60`."
solution: |
minutes = 135
hours = minutes // 60
remaining = minutes % 60
print(hours, "h", remaining, "min")
- type: explain
voice: true
text:
fr: "Python respecte les priorités, comme en maths : d'abord `**`, puis `*` `/` `//` `%`, puis `+` et `-`. Les parenthèses passent avant tout."
en: "Python follows priorities, like in maths: first `**`, then `*` `/` `//` `%`, then `+` and `-`. Parentheses come before everything."
- type: code
starter: |
result = 2 + 3 * 4
print(result)
instruction:
fr: Ce code affiche 14. Ajoute des parenthèses pour qu'il affiche 20.
en: This code shows 14. Add parentheses so that it shows 20.
tests:
- kind: var_equals
name: result
value: 20
- kind: stdout_equals
value: "20\n"
hints:
fr:
- "Il faut que l'addition soit calculée en premier."
- "Écris `(2 + 3) * 4`."
en:
- "The addition has to be calculated first."
- "Write `(2 + 3) * 4`."
solution: |
result = (2 + 3) * 4
print(result)
mission:
title:
fr: Convertisseur d'unités
en: Unit converter
goal:
fr: "Demande des degrés Celsius et une distance en km (avec `float(input(...))`). Calcule `fahrenheit` (celsius * 9 / 5 + 32) et `miles` (1 km = 0.621371 mile), puis affiche les deux résultats."
en: "Ask for degrees Celsius and a distance in km (with `float(input(...))`). Compute `fahrenheit` (celsius * 9 / 5 + 32) and `miles` (1 km = 0.621371 mile), then print both results."
starter: |
# Demande les deux valeurs, puis calcule fahrenheit et miles
tests:
- kind: var_equals
name: fahrenheit
value: 212
inputs:
- "100"
- "10"
message:
fr: "Avec 100 °C, `fahrenheit` doit valoir 212."
en: "With 100 °C, `fahrenheit` must be 212."
- kind: var_equals
name: miles
value: 6.21371
inputs:
- "100"
- "10"
message:
fr: "Avec 10 km, `miles` doit valoir 6.21371."
en: "With 10 km, `miles` must be 6.21371."
- kind: var_equals
name: fahrenheit
value: 32
inputs:
- "0"
- "1"
message:
fr: "Avec 0 °C, `fahrenheit` doit valoir 32."
en: "With 0 °C, `fahrenheit` must be 32."
- kind: stdout_contains
value: "212"
inputs:
- "100"
- "10"
message:
fr: Le programme doit afficher les résultats.
en: The program must print the results.
solution: |
celsius = float(input("Température en °C ? "))
km = float(input("Distance en km ? "))
fahrenheit = celsius * 9 / 5 + 32
miles = km * 0.621371
print(fahrenheit, "°F")
print(miles, "miles")
reward_sound: success_small
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+30
View File
@@ -0,0 +1,30 @@
{
"en": {
"6fea2cc5252c6f6f": {
"text": "A comparison asks a question whose answer is True or False. We compare with == (equal), != (different), <, >, <= and >=. Careful: = stores a value, == compares.",
"voice": "en_US-ljspeech-high"
},
"9fcb323dcc1d8114": {
"text": "For more than two cases, add elif (else if). Python tests the conditions in order and stops at the first true one. You can also combine them with and, or and not.",
"voice": "en_US-ljspeech-high"
},
"e2c1d81c6f239864": {
"text": "With if, the program chooses what to do. The line ends with a colon, and the block that follows is shifted by 4 spaces. else gives the other case.",
"voice": "en_US-ljspeech-high"
}
},
"fr": {
"1d0b4f20705e76da": {
"text": "Une comparaison pose une question dont la réponse est True (vrai) ou False (faux). On compare avec == (égal), != (différent), <, >, <= et >=. Attention : = range une valeur, == compare.",
"voice": "fr_FR-siwis-medium"
},
"e4a397a8d71a6682": {
"text": "Avec if, le programme choisit quoi faire. La ligne finit par deux-points, et le bloc qui suit est décalé de 4 espaces. else donne l'autre cas.",
"voice": "fr_FR-siwis-medium"
},
"f3ef37b4feceb432": {
"text": "Pour plus de deux cas, ajoute elif (sinon si). Python teste les conditions dans l'ordre et s'arrête à la première vraie. Tu peux aussi combiner avec and (et), or (ou) et not (non).",
"voice": "fr_FR-siwis-medium"
}
}
}
+186
View File
@@ -0,0 +1,186 @@
id: 05-decide
title:
fr: Décider
en: Deciding
difficulty: 2
xp: 50
requires:
- 04-math
steps:
- type: explain
voice: true
text:
fr: "Une comparaison pose une question dont la réponse est `True` (vrai) ou `False` (faux). On compare avec `==` (égal), `!=` (différent), `<`, `>`, `<=` et `>=`. Attention : `=` range une valeur, `==` compare."
en: "A comparison asks a question whose answer is `True` or `False`. We compare with `==` (equal), `!=` (different), `<`, `>`, `<=` and `>=`. Careful: `=` stores a value, `==` compares."
- type: code
starter: |
age = 15
print(age >= 18)
instruction:
fr: Ce code affiche False. Change la valeur de `age` pour qu'il affiche True.
en: This code shows False. Change the value of `age` so that it shows True.
tests:
- kind: stdout_equals
value: "True\n"
- kind: var_type_is
name: age
value: int
hints:
fr:
- "Il faut que `age` soit au moins égal à 18."
en:
- "`age` has to be at least 18."
solution: |
age = 20
print(age >= 18)
- type: explain
voice: true
text:
fr: "Avec `if`, le programme choisit quoi faire. La ligne finit par deux-points, et le bloc qui suit est décalé de 4 espaces. `else` donne l'autre cas."
en: "With `if`, the program chooses what to do. The line ends with a colon, and the block that follows is shifted by 4 spaces. `else` gives the other case."
- type: code
starter: |
age = int(input("Ton âge ? "))
if age < 18:
print("Mineur")
else:
print("Majeur")
instruction:
fr: "Transforme ce programme : il doit afficher Gratuit si l'âge est inférieur à 12, et Payant sinon."
en: "Change this program: it must show Gratuit if the age is under 12, and Payant otherwise."
tests:
- kind: stdout_matches_regex
value: "^[^\\n]*Gratuit\\n$"
inputs:
- "8"
message:
fr: "Avec 8 ans, il faut afficher Gratuit."
en: "With age 8, it must show Gratuit."
- kind: stdout_matches_regex
value: "^[^\\n]*Payant\\n$"
inputs:
- "30"
message:
fr: "Avec 30 ans, il faut afficher Payant."
en: "With age 30, it must show Payant."
- kind: stdout_matches_regex
value: "^[^\\n]*Payant\\n$"
inputs:
- "12"
message:
fr: "À 12 ans, c'est déjà Payant."
en: "At 12, it is already Payant."
hints:
fr:
- "Change le nombre dans la condition et les deux textes."
- "`if age < 12:` puis `print(\"Gratuit\")`, et `print(\"Payant\")` dans le `else`."
en:
- "Change the number in the condition and both texts."
- "`if age < 12:` then `print(\"Gratuit\")`, and `print(\"Payant\")` in the `else`."
solution: |
age = int(input("Ton âge ? "))
if age < 12:
print("Gratuit")
else:
print("Payant")
- type: explain
voice: true
text:
fr: "Pour plus de deux cas, ajoute `elif` (sinon si). Python teste les conditions dans l'ordre et s'arrête à la première vraie. Tu peux aussi combiner avec `and` (et), `or` (ou) et `not` (non)."
en: "For more than two cases, add `elif` (else if). Python tests the conditions in order and stops at the first true one. You can also combine them with `and`, `or` and `not`."
- type: code
starter: |
score = int(input("Ta note sur 20 ? "))
print("À compléter")
instruction:
fr: "Avec `if`, `elif` et `else`, affiche Excellent (16 ou plus), Bien (12 ou plus), Passable (10 ou plus), sinon Insuffisant."
en: "With `if`, `elif` and `else`, show Excellent (16 or more), Bien (12 or more), Passable (10 or more), otherwise Insuffisant."
tests:
- kind: stdout_matches_regex
value: "^[^\\n]*Excellent\\n$"
inputs:
- "18"
message:
fr: "Avec 18, il faut afficher Excellent."
en: "With 18, it must show Excellent."
- kind: stdout_matches_regex
value: "^[^\\n]*Bien\\n$"
inputs:
- "13"
message:
fr: "Avec 13, il faut afficher Bien."
en: "With 13, it must show Bien."
- kind: stdout_matches_regex
value: "^[^\\n]*Passable\\n$"
inputs:
- "10"
message:
fr: "Avec 10, il faut afficher Passable."
en: "With 10, it must show Passable."
- kind: stdout_matches_regex
value: "^[^\\n]*Insuffisant\\n$"
inputs:
- "4"
message:
fr: "Avec 4, il faut afficher Insuffisant."
en: "With 4, it must show Insuffisant."
hints:
fr:
- "Commence par la condition la plus exigeante : `if score >= 16:`."
- "Enchaîne avec `elif score >= 12:` puis `elif score >= 10:` et termine par `else:`."
en:
- "Start with the most demanding condition: `if score >= 16:`."
- "Continue with `elif score >= 12:` then `elif score >= 10:` and finish with `else:`."
solution: |
score = int(input("Ta note sur 20 ? "))
if score >= 16:
print("Excellent")
elif score >= 12:
print("Bien")
elif score >= 10:
print("Passable")
else:
print("Insuffisant")
mission:
title:
fr: Le nombre mystère
en: The mystery number
goal:
fr: "Le nombre secret est 7. Demande un choix entre 1 et 10, puis affiche Bravo si c'est le bon, Trop petit si le choix est plus petit, Trop grand sinon. Un seul essai !"
en: "The secret number is 7. Ask for a choice between 1 and 10, then show Bravo if it is right, Trop petit if the choice is smaller, Trop grand otherwise. One try only!"
starter: |
secret = 7
guess = int(input("Ton choix (1-10) ? "))
# Compare guess et secret
tests:
- kind: stdout_matches_regex
value: "^[^\\n]*Bravo\\n$"
inputs:
- "7"
message:
fr: "Avec 7, il faut afficher Bravo."
en: "With 7, it must show Bravo."
- kind: stdout_matches_regex
value: "^[^\\n]*Trop petit\\n$"
inputs:
- "3"
message:
fr: "Avec 3, il faut afficher Trop petit."
en: "With 3, it must show Trop petit."
- kind: stdout_matches_regex
value: "^[^\\n]*Trop grand\\n$"
inputs:
- "9"
message:
fr: "Avec 9, il faut afficher Trop grand."
en: "With 9, it must show Trop grand."
solution: |
secret = 7
guess = int(input("Ton choix (1-10) ? "))
if guess == secret:
print("Bravo")
elif guess < secret:
print("Trop petit")
else:
print("Trop grand")
reward_sound: success_small
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+38
View File
@@ -0,0 +1,38 @@
{
"en": {
"77e4a68d5c84d685": {
"text": "A list stores several values between square brackets: fruits = [\"apple\", \"pear\"]. We read an item with its position, which starts at 0: fruits[0] is \"apple\". len(fruits) gives the size.",
"voice": "en_US-ljspeech-high"
},
"ab4815897e7a1a8a": {
"text": "A loop can also build up a result. We prepare a variable at 0, then add each item to it: total = total + n. To repeat a fixed number of times, for i in range(3): makes three turns.",
"voice": "en_US-ljspeech-high"
},
"af52d6975b6bf775": {
"text": "To pick at random, Python has a random module. We import it with import random, then random.choice(list) returns an item picked at random. Each run can give a different result.",
"voice": "en_US-ljspeech-high"
},
"f06ad642fd408e38": {
"text": "To handle every item, we use for. for fruit in fruits: repeats the indented block, once per item, putting the item in fruit.",
"voice": "en_US-ljspeech-high"
}
},
"fr": {
"33efc1d313b78e67": {
"text": "Pour traiter chaque élément, on utilise for. for fruit in fruits: répète le bloc décalé, une fois par élément, en mettant l'élément dans fruit.",
"voice": "fr_FR-siwis-medium"
},
"512174193b912753": {
"text": "Pour tirer au hasard, Python a un module random. On l'importe avec import random, puis random.choice(liste) renvoie un élément pris au hasard. Chaque exécution peut donner un résultat différent.",
"voice": "fr_FR-siwis-medium"
},
"b6a7a3515f42c372": {
"text": "Une boucle peut aussi accumuler un résultat. On prépare une variable à 0, puis on y ajoute chaque élément : total = total + n. Pour répéter un nombre fixe de fois, for i in range(3): fait trois tours.",
"voice": "fr_FR-siwis-medium"
},
"cc9727a5aaabaf51": {
"text": "Une liste range plusieurs valeurs entre crochets : fruits = [\"pomme\", \"poire\"]. On lit un élément avec sa position, qui commence à 0 : fruits[0] est \"pomme\". len(fruits) donne la taille.",
"voice": "fr_FR-siwis-medium"
}
}
}
+176
View File
@@ -0,0 +1,176 @@
id: 06-lists
title:
fr: Répéter avec une liste
en: Repeating with a list
difficulty: 2
xp: 60
requires:
- 05-decide
steps:
- type: explain
voice: true
text:
fr: "Une liste range plusieurs valeurs entre crochets : `fruits = [\"pomme\", \"poire\"]`. On lit un élément avec sa position, qui commence à 0 : `fruits[0]` est \"pomme\". `len(fruits)` donne la taille."
en: "A list stores several values between square brackets: `fruits = [\"apple\", \"pear\"]`. We read an item with its position, which starts at 0: `fruits[0]` is \"apple\". `len(fruits)` gives the size."
- type: code
starter: |
fruits = ["pomme", "poire", "cerise"]
print(fruits[0])
instruction:
fr: Ce code affiche le premier fruit. Modifie-le pour afficher le dernier, cerise.
en: This code shows the first fruit. Change it to show the last one, cerise.
tests:
- kind: stdout_equals
value: "cerise\n"
hints:
fr:
- "Les positions sont 0, 1 et 2."
- "Utilise `fruits[2]`, ou `fruits[-1]` pour le dernier."
en:
- "The positions are 0, 1 and 2."
- "Use `fruits[2]`, or `fruits[-1]` for the last one."
solution: |
fruits = ["pomme", "poire", "cerise"]
print(fruits[2])
- type: explain
voice: true
text:
fr: "Pour traiter chaque élément, on utilise `for`. `for fruit in fruits:` répète le bloc décalé, une fois par élément, en mettant l'élément dans `fruit`."
en: "To handle every item, we use `for`. `for fruit in fruits:` repeats the indented block, once per item, putting the item in `fruit`."
- type: code
starter: |
fruits = ["pomme", "poire", "cerise"]
for fruit in fruits:
print(fruit)
instruction:
fr: "Change le `print` pour afficher J'aime pomme, J'aime poire, J'aime cerise (une ligne par fruit)."
en: "Change the `print` to show J'aime pomme, J'aime poire, J'aime cerise (one line per fruit)."
tests:
- kind: stdout_equals
value: "J'aime pomme\nJ'aime poire\nJ'aime cerise\n"
- kind: code_contains
value: "for "
message:
fr: Utilise une boucle for.
en: Use a for loop.
hints:
fr:
- "Mets plusieurs valeurs dans `print`, séparées par des virgules."
- "`print(\"J'aime\", fruit)`"
en:
- "Put several values in `print`, separated by commas."
- "`print(\"J'aime\", fruit)`"
solution: |
fruits = ["pomme", "poire", "cerise"]
for fruit in fruits:
print("J'aime", fruit)
- type: explain
voice: true
text:
fr: "Une boucle peut aussi accumuler un résultat. On prépare une variable à 0, puis on y ajoute chaque élément : `total = total + n`. Pour répéter un nombre fixe de fois, `for i in range(3):` fait trois tours."
en: "A loop can also build up a result. We prepare a variable at 0, then add each item to it: `total = total + n`. To repeat a fixed number of times, `for i in range(3):` makes three turns."
- type: code
starter: |
numbers = [4, 8, 15, 16]
total = 0
for n in numbers:
pass
print(total)
instruction:
fr: "Remplace `pass` pour que `total` contienne la somme des nombres (43)."
en: "Replace `pass` so that `total` holds the sum of the numbers (43)."
tests:
- kind: var_equals
name: total
value: 43
- kind: stdout_equals
value: "43\n"
hints:
fr:
- "À chaque tour, ajoute `n` à `total`."
- "`total = total + n`"
en:
- "At each turn, add `n` to `total`."
- "`total = total + n`"
solution: |
numbers = [4, 8, 15, 16]
total = 0
for n in numbers:
total = total + n
print(total)
- type: explain
voice: true
text:
fr: "Pour tirer au hasard, Python a un module `random`. On l'importe avec `import random`, puis `random.choice(liste)` renvoie un élément pris au hasard. Chaque exécution peut donner un résultat différent."
en: "To pick at random, Python has a `random` module. We import it with `import random`, then `random.choice(list)` returns an item picked at random. Each run can give a different result."
- type: code
starter: |
import random
colors = ["rouge", "bleu", "vert"]
color = colors[0]
print(color)
instruction:
fr: Utilise `random.choice` pour choisir la couleur au hasard. Exécute plusieurs fois pour voir le résultat changer.
en: Use `random.choice` to pick the color at random. Run it several times to see the result change.
tests:
- kind: code_contains
value: random.choice
message:
fr: Utilise random.choice(colors).
en: Use random.choice(colors).
- kind: stdout_matches_regex
value: "^(rouge|bleu|vert)\\n$"
hints:
fr:
- "Remplace `colors[0]` par un appel à `random.choice`."
- "`color = random.choice(colors)`"
en:
- "Replace `colors[0]` with a call to `random.choice`."
- "`color = random.choice(colors)`"
solution: |
import random
colors = ["rouge", "bleu", "vert"]
color = random.choice(colors)
print(color)
mission:
title:
fr: Générateur de mots de passe
en: Password generator
goal:
fr: "Construis dans `password` trois mots tirés au hasard dans `words`, séparés par des tirets (par exemple lune-chat-vent), avec une boucle for et `random.choice`. Puis affiche-le."
en: "Build in `password` three words picked at random from `words`, separated by dashes (for example lune-chat-vent), with a for loop and `random.choice`. Then print it."
starter: |
import random
words = ["chat", "lune", "pomme", "soleil", "vent"]
password = ""
# Ajoute 3 mots tirés au hasard, séparés par des tirets
print(password)
tests:
- kind: stdout_matches_regex
value: "^(chat|lune|pomme|soleil|vent)(-(chat|lune|pomme|soleil|vent)){2}\\n$"
message:
fr: "Le mot de passe doit contenir exactement 3 mots de la liste séparés par des tirets."
en: "The password must contain exactly 3 words from the list separated by dashes."
- kind: var_type_is
name: password
value: str
- kind: code_contains
value: random.choice
message:
fr: Utilise random.choice(words).
en: Use random.choice(words).
- kind: code_contains
value: "for "
message:
fr: Utilise une boucle for.
en: Use a for loop.
solution: |
import random
words = ["chat", "lune", "pomme", "soleil", "vent"]
password = ""
for i in range(3):
if i > 0:
password = password + "-"
password = password + random.choice(words)
print(password)
reward_sound: success_big
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+38
View File
@@ -0,0 +1,38 @@
{
"en": {
"034c78ada8b29572": {
"text": "while repeats a block as long as a condition is true. Python tests the condition before each turn. As soon as it becomes false, the loop stops.",
"voice": "en_US-ljspeech-high"
},
"62833437feb2d118": {
"text": "For the mission, your game will compare the choice with the secret number, with an attempts counter. When it works, replace secret = 7 with import random and secret = random.randint(1, 10) to really play.",
"voice": "en_US-ljspeech-high"
},
"6b7bb567006dc055": {
"text": "Beware of the infinite loop! If the condition never becomes false, for example if you forget count = count - 1, the program runs forever. In that case, click Stop. To avoid it, always check that something changes inside the loop.",
"voice": "en_US-ljspeech-high"
},
"ad16cce1989900ce": {
"text": "Sometimes we don't know in advance how many turns will be needed, for example until the user finds the answer. We then write while True: and leave the loop with break at the right moment.",
"voice": "en_US-ljspeech-high"
}
},
"fr": {
"1bbc90eeb69a0608": {
"text": "Parfois, on ne sait pas à l'avance combien de tours il faudra, par exemple tant que l'utilisateur n'a pas trouvé. On écrit alors while True: et on sort de la boucle avec break quand c'est le bon moment.",
"voice": "fr_FR-siwis-medium"
},
"79d5a83c13821a3b": {
"text": "Pour la mission, ton jeu comparera le choix au nombre secret, compteur d'essais en plus. Quand il fonctionnera, remplace secret = 7 par import random et secret = random.randint(1, 10) pour jouer pour de vrai.",
"voice": "fr_FR-siwis-medium"
},
"c38225571258a2d0": {
"text": "while répète un bloc tant qu'une condition est vraie. Python teste la condition avant chaque tour. Dès qu'elle devient fausse, la boucle s'arrête.",
"voice": "fr_FR-siwis-medium"
},
"d221fc8d58c319da": {
"text": "Attention à la boucle infinie ! Si la condition ne devient jamais fausse, par exemple si tu oublies count = count - 1, le programme tourne sans fin. Dans ce cas, clique sur Arrêter. Pour l'éviter, vérifie toujours que quelque chose change dans la boucle.",
"voice": "fr_FR-siwis-medium"
}
}
}
+184
View File
@@ -0,0 +1,184 @@
id: 07-while
title:
fr: Répéter avec une condition
en: Repeating with a condition
difficulty: 3
xp: 70
requires:
- 06-lists
steps:
- type: explain
voice: true
text:
fr: "`while` répète un bloc tant qu'une condition est vraie. Python teste la condition avant chaque tour. Dès qu'elle devient fausse, la boucle s'arrête."
en: "`while` repeats a block as long as a condition is true. Python tests the condition before each turn. As soon as it becomes false, the loop stops."
- type: code
starter: |
count = 3
while count > 0:
print(count)
count = count - 1
print("Partez !")
instruction:
fr: Exécute ce compte à rebours, puis fais-le partir de 5.
en: Run this countdown, then make it start from 5.
tests:
- kind: stdout_equals
value: "5\n4\n3\n2\n1\nPartez !\n"
hints:
fr:
- "Change seulement la première ligne."
en:
- "Only change the first line."
solution: |
count = 5
while count > 0:
print(count)
count = count - 1
print("Partez !")
- type: explain
voice: true
text:
fr: "Attention à la boucle infinie ! Si la condition ne devient jamais fausse, par exemple si tu oublies `count = count - 1`, le programme tourne sans fin. Dans ce cas, clique sur Arrêter. Pour l'éviter, vérifie toujours que quelque chose change dans la boucle."
en: "Beware of the infinite loop! If the condition never becomes false, for example if you forget `count = count - 1`, the program runs forever. In that case, click Stop. To avoid it, always check that something changes inside the loop."
- type: code
starter: |
total = 0
number = 1
while total < 20:
total = total + number
number = number + 1
print(total, number)
instruction:
fr: "Cette boucle additionne 1, 2, 3… jusqu'à dépasser 20. Change la limite pour qu'elle s'arrête dès que le total atteint au moins 50."
en: "This loop adds 1, 2, 3… until it goes past 20. Change the limit so it stops as soon as the total reaches at least 50."
tests:
- kind: var_equals
name: total
value: 55
- kind: var_equals
name: number
value: 11
hints:
fr:
- "La condition de la boucle contient la limite."
- "Écris `while total < 50:`."
en:
- "The loop condition contains the limit."
- "Write `while total < 50:`."
solution: |
total = 0
number = 1
while total < 50:
total = total + number
number = number + 1
print(total, number)
- type: explain
voice: true
text:
fr: "Parfois, on ne sait pas à l'avance combien de tours il faudra, par exemple tant que l'utilisateur n'a pas trouvé. On écrit alors `while True:` et on sort de la boucle avec `break` quand c'est le bon moment."
en: "Sometimes we don't know in advance how many turns will be needed, for example until the user finds the answer. We then write `while True:` and leave the loop with `break` at the right moment."
- type: code
starter: |
while True:
word = input("Mot de passe ? ")
print("Essaie encore")
break
print("Bienvenue")
instruction:
fr: "Le mot de passe est python. Redemande tant que ce n'est pas le bon, en affichant Essaie encore. Quand c'est bon, sors de la boucle : le programme affiche Bienvenue."
en: "The password is python. Ask again until it is right, showing Essaie encore. When it is right, leave the loop: the program shows Bienvenue."
tests:
- kind: stdout_matches_regex
value: "^[^\\n]*Bienvenue\\n$"
inputs:
- python
message:
fr: "Avec le bon mot de passe du premier coup, il ne faut afficher que Bienvenue."
en: "With the right password at the first try, only Bienvenue must be shown."
- kind: stdout_matches_regex
value: "^[^\\n]*Essaie encore\\n[^\\n]*Bienvenue\\n$"
inputs:
- nope
- python
message:
fr: "Avec un mauvais mot de passe puis le bon, il faut afficher Essaie encore puis Bienvenue."
en: "With a wrong password then the right one, it must show Essaie encore then Bienvenue."
hints:
fr:
- "Teste le mot avec `if word == \"python\":` et mets `break` dedans."
- "Mets le `print(\"Essaie encore\")` dans le `else`."
en:
- "Test the word with `if word == \"python\":` and put `break` inside."
- "Put the `print(\"Essaie encore\")` in the `else`."
solution: |
while True:
word = input("Mot de passe ? ")
if word == "python":
break
else:
print("Essaie encore")
print("Bienvenue")
- type: explain
voice: true
text:
fr: "Pour la mission, ton jeu comparera le choix au nombre secret, compteur d'essais en plus. Quand il fonctionnera, remplace `secret = 7` par `import random` et `secret = random.randint(1, 10)` pour jouer pour de vrai."
en: "For the mission, your game will compare the choice with the secret number, with an attempts counter. When it works, replace `secret = 7` with `import random` and `secret = random.randint(1, 10)` to really play."
mission:
title:
fr: Le nombre mystère, version complète
en: The mystery number, full version
goal:
fr: "Le nombre secret est 7. Redemande un choix tant que ce n'est pas le bon. Affiche Trop petit ou Trop grand à chaque erreur, compte les essais dans `attempts`, et affiche Bravo quand c'est trouvé."
en: "The secret number is 7. Ask again until the choice is right. Show Trop petit or Trop grand after each mistake, count the tries in `attempts`, and show Bravo when found."
starter: |
secret = 7
attempts = 0
# Boucle : redemande jusqu'à trouver le nombre secret
tests:
- kind: var_equals
name: attempts
value: 3
inputs:
- "3"
- "9"
- "7"
message:
fr: "Avec les choix 3, 9 puis 7, `attempts` doit valoir 3."
en: "With the choices 3, 9 then 7, `attempts` must be 3."
- kind: stdout_matches_regex
value: "^[^\\n]*Trop petit\\n[^\\n]*Trop grand\\n[^\\n]*Bravo[^\\n]*\\n$"
inputs:
- "3"
- "9"
- "7"
message:
fr: "Avec 3, 9 puis 7, il faut afficher Trop petit, Trop grand, puis Bravo."
en: "With 3, 9 then 7, it must show Trop petit, Trop grand, then Bravo."
- kind: var_equals
name: attempts
value: 1
inputs:
- "7"
message:
fr: "Avec 7 du premier coup, `attempts` doit valoir 1."
en: "With 7 at the first try, `attempts` must be 1."
- kind: code_contains
value: while
message:
fr: Utilise une boucle while.
en: Use a while loop.
solution: |
secret = 7
attempts = 0
while True:
guess = int(input("Ton choix (1-10) ? "))
attempts = attempts + 1
if guess == secret:
print("Bravo, trouvé en", attempts, "essais")
break
elif guess < secret:
print("Trop petit")
else:
print("Trop grand")
reward_sound: success_big
+5 -1
View File
@@ -4,6 +4,10 @@
{ "id": "00-intro", "requires": [] },
{ "id": "01-hello", "requires": ["00-intro"] },
{ "id": "02-variables", "requires": ["01-hello"] },
{ "id": "03-input", "requires": ["02-variables"] }
{ "id": "03-input", "requires": ["02-variables"] },
{ "id": "04-math", "requires": ["03-input"] },
{ "id": "05-decide", "requires": ["04-math"] },
{ "id": "06-lists", "requires": ["05-decide"] },
{ "id": "07-while", "requires": ["06-lists"] }
]
}
+5
View File
@@ -117,6 +117,7 @@
"stdout_matches_regex",
"var_equals",
"var_type_is",
"code_contains",
"function_called_with",
"no_syntax_error"
]
@@ -147,6 +148,10 @@
"type": "array",
"items": { "$ref": "#/$defs/test" }
},
"solution": {
"type": "string",
"description": "Code de référence de la mission. Non affiché à l'apprenant : sert aux tests automatiques (make test vérifie que la solution passe tous les tests)."
},
"reward_sound": {
"type": "string",
"enum": ["success_small", "success_big", "level_unlocked"],