Objectif : réviser les bases (variables, boucles, conditions, fonctions) en manipulant des listes et des tuples.
append, remove, pop, len, boucle fore[0], e[1]if, affichage print, fonctions simplesÀ copier dans l’IDE Edupython ou sur BASTHON :
# Liste (modifiable)
cadeaux = ["livre", "chocolat", "jeu"]
# Tuple (non modifiable)
enfant = ("Alice", 16)
# Boucle
for cadeau in cadeaux:
print(cadeau)
# Condition
if "chocolat" in cadeaux:
print("Miam 🎄")
len(cadeaux), cadeaux[0]hotte = ["PS5", "pull de Noël", "chocolats", "livre"]
"bonnet""pull de Noël"# afficher
for c in hotte:
print(c)
# ajouter
hotte.append("bonnet")
# supprimer
hotte.remove("pull de Noël")
# compter
print(len(hotte))
Chaque enfant : (prenom, sage) où sage vaut True ou False.
enfant1 = ("Léo", True)
enfant2 = ("Emma", False)
enfant3 = ("Noah", True)
enfants = [enfant1, enfant2, enfant3]
for e in enfants:
prenom = e[0]
sage = e[1]
if sage:
print(prenom, "aura un cadeau 🎁")
else:
print(prenom, "aura du charbon 🪨")
def distribution(enfant):
def distribution(enfant):
for e in enfants:
prenom = e[0]
sage = e[1]
if sage:
print(prenom, "aura un cadeau 🎁")
else:
print(prenom, "aura du charbon 🪨")