diff --git a/tirkdo.py b/tirkdo.py index 91633e7..d1a3d4b 100644 --- a/tirkdo.py +++ b/tirkdo.py @@ -14,8 +14,6 @@ participants = [ 'Ludo', ] -participants_recois = participants.copy() - on_evite = { 'Ben': 'Ludo', 'Charlotte': 'Nico', @@ -23,29 +21,62 @@ on_evite = { 'Didi': 'Johnny', } -# On mélange les listes -random.shuffle(participants) -random.shuffle(participants_recois) +annee_passee = { + 'Nico': 'Ben', + 'Didi': 'Nico', + 'Mémé': 'Papa', + 'Maman': 'Mémé', + 'Charlotte': 'Ludo', + 'Johnny': 'Charlotte', + 'Papa': 'Didi', + 'Ludo': 'Maman', + 'Ben': 'Johnny', +} + +class EchecTirage(Exception): + pass + +# On défine notre méthode de tirage +def tirage(nb_tentatives_max=99): + participants_offre = participants.copy() + participants_recois = participants.copy() + + # On mélange les listes + random.shuffle(participants_offre) + random.shuffle(participants_recois) + + result = dict() + for offre in participants_offre: + count = 0 + while True: + count += 1 + if count == nb_tentatives_max: + raise EchecTirage + recois = random.choice(participants_recois) + # Pas à soi même + if recois == offre: + continue + # On évite entre couple + if on_evite.get(offre) == recois or on_evite.get(recois) == offre: + continue + # On évite de faire comme l'an passée + if annee_passee.get(offre) == recois: + continue + # Trouvé ! + result[offre] = recois + participants_recois.remove(recois) + break + return result # On procède au tirage -tirage = dict() -for offre in participants: - while True: - recois = random.choice(participants_recois) - - # Pas à soi même - if recois == offre: - continue - # On évite entre couple - if on_evite.get(offre) == recois or on_evite.get(recois) == offre: - continue - - # Trouvé ! - tirage[offre] = recois - participants_recois.remove(recois) - break - +result = None +while not result: + try: + result = tirage() + except EchecTirage: + print("Échec du tirage, on recommence !") + result = None print("Résultat du tirage :") -for offre, recois in tirage.items(): - print(" - %s -> %s" % (offre, recois)) +for offre, recois in result.items(): + print(f" - {offre} -> {recois}")