quantities = [4, -2, 0, 7]
total = 0
rejected_indexes = []
for index, quantity in enumerate(quantities):
    if quantity < 0:
        rejected_indexes.append(index)
        continue
    total += quantity
assert total == 11
assert rejected_indexes == [1]
assert quantities == [4, -2, 0, 7]


taille_lot = 7
compteur = 0
totaux = []
while compteur < 10:
    totaux.append((compteur + 1) * taille_lot)
    compteur += 1
assert totaux == [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
assert compteur == 10

totaux_for = []
for numero in range(1, 11):
    totaux_for.append(numero * taille_lot)
assert totaux_for == totaux

identifiant = "A12"
masque = []
for caractere in identifiant:
    if caractere in "0123456789":
        masque.append(caractere)
    else:
        masque.append("*")
assert "".join(masque) == "*12"


compteur = 1
positions = []
while compteur < 20:
    if compteur % 3 == 0:
        compteur += 4
        continue
    positions.append(compteur)
    compteur += 1
assert positions == [1, 2, 7, 8, 13, 14, 19]

commandes = ["lire", "Q", "ignorer"]
traitees = []
for commande in commandes:
    if commande == "Q":
        break
    traitees.append(commande)
assert traitees == ["lire"]

def lire_messages(messages):
    recus = []
    for message in messages:
        if message == "FIN":
            break
        recus.append(message)
    return recus

assert lire_messages(["lot-A", "FIN", "lot-B"]) == ["lot-A"]
assert lire_messages(["lot-A", "lot-B"]) == ["lot-A", "lot-B"]
assert lire_messages([]) == []
compteur_vide = 10
visites = []
while compteur_vide < 10:
    visites.append(compteur_vide)
    compteur_vide += 1
assert visites == []
assert "O" not in "0123456789" and "0" in "0123456789"

def chercher_quantite(lots, identifiant):
    for lot in lots:
        if lot["id"] == identifiant:
            resultat = lot["quantite"]
            if resultat is None:
                raise ValueError("Quantité manquante pour le lot trouvé")
            break
    else:
        return None
    return resultat

lots_test = [{"id": "A", "quantite": 0}, {"id": "B", "quantite": 4}]
assert chercher_quantite(lots_test, "A") == 0
assert chercher_quantite(lots_test, "B") == 4
assert chercher_quantite(lots_test, "C") is None
assert chercher_quantite([], "A") is None
try:
    chercher_quantite([{"id": "A", "quantite": None}], "A")
except ValueError:
    pass
else:
    raise AssertionError("Une quantité absente ne doit pas masquer un lot trouvé")

for caractere_lot in "A12":
    pass
assert caractere_lot == "2"
derniere_ligne = "aucune ligne"
for derniere_ligne in ():
    pass
assert derniere_ligne == "aucune ligne"

import unicodedata

identifiant_decompose = "Cafe\u0301"
identifiant_precompose = "Caf\u00e9"
assert identifiant_decompose != identifiant_precompose
assert len(identifiant_decompose) == 5
assert len(identifiant_precompose) == 4
assert unicodedata.normalize("NFC", identifiant_decompose) == identifiant_precompose

from contextlib import redirect_stdout
from io import StringIO

sortie_fixe = StringIO()
with redirect_stdout(sortie_fixe):
    print(1 * 7)
    print(2 * 7)
    print(3 * 7)
    print(4 * 7)
    print(5 * 7)
    print(6 * 7)
    print(7 * 7)
    print(8 * 7)
    print(9 * 7)
    print(10 * 7)

taille_lot = 7
sortie_parametree = StringIO()
with redirect_stdout(sortie_parametree):
    print(1 * taille_lot)
    print(2 * taille_lot)
    print(3 * taille_lot)
    print(4 * taille_lot)
    print(5 * taille_lot)
    print(6 * taille_lot)
    print(7 * taille_lot)
    print(8 * taille_lot)
    print(9 * taille_lot)
    print(10 * taille_lot)

attendus = [str(numero * taille_lot) for numero in range(1, 11)]
assert sortie_fixe.getvalue().splitlines() == attendus
assert sortie_parametree.getvalue().splitlines() == attendus
assert [str(total) for total in totaux] == attendus

def totaux_saisis(texte_taille):
    taille = int(texte_taille)  # ValueError si le texte n'est pas un entier
    if taille < 0:
        raise ValueError("La taille du lot doit être positive ou nulle")
    i = 0
    resultats = []
    while i < 10:
        resultats.append((i + 1) * taille)
        i += 1
    return resultats

# En utilisation interactive : totaux_saisis(input("Taille du lot : "))
assert totaux_saisis("3") == [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
assert totaux_saisis("0") == [0] * 10
for texte_invalide in ("trois", "-1"):
    try:
        totaux_saisis(texte_invalide)
    except ValueError:
        pass
    else:
        raise AssertionError("La taille invalide aurait dû être refusée")

def saisir_identifiants(lire=input):
    identifiants = []
    while True:
        try:
            commande = lire("Identifiant du lot, ou Q pour terminer : ")
        except EOFError:
            break
        if commande == "Q":
            break
        identifiants.append(commande)
    return identifiants

# Dans un terminal : identifiants = saisir_identifiants()
commandes_test = iter(["lot-A", "lot-B", "Q", "lot-C"])
assert saisir_identifiants(lambda invite: next(commandes_test)) == ["lot-A", "lot-B"]
assert next(commandes_test) == "lot-C"

def entree_fermee(invite):
    raise EOFError

assert saisir_identifiants(entree_fermee) == []

lots_imbriques = [("A", [1, -1, 9]), ("B", [3, 0, 4])]
lignes_retenues = []
for nom_lot, quantites in lots_imbriques:
    for quantite in quantites:
        if quantite < 0:
            break
        lignes_retenues.append((nom_lot, quantite))
assert lignes_retenues == [("A", 1), ("B", 3), ("B", 0), ("B", 4)]

taille_lot_variante = 7
numero_lot = 1
totaux_depuis_un = []
while numero_lot <= 10:
    totaux_depuis_un.append(numero_lot * taille_lot_variante)
    numero_lot += 1
assert numero_lot == 11
assert totaux_depuis_un == totaux == [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
