eng
competition

Text Practice Mode

Python commandes et structures courantes

created Sunday October 19, 07:52 by julienfff


0


Rating

403 words
8 completed
00:00
import random
import os
 
FICHIER = "citations.txt"
 
def charger_citations():
    if not os.path.exists(FICHIER):
        return [
            "Le savoir est une arme.",
            "Chaque jour compte.",
            "Rien n'est impossible.",
            "Apprendre, c'est grandir."
        ]
    with open(FICHIER, "r", encoding="utf-8") as f:
        return [ligne.strip() for ligne in f if ligne.strip()]
 
def sauvegarder_citations(citations):
    with open(FICHIER, "w", encoding="utf-8") as f:
        for c in citations:
            f.write(c + "\n")
 
def afficher_menu():
    print("\n=== GÉNÉRATEUR DE CITATIONS ===")
    print("1. Afficher une citation aléatoire")
    print("2. Ajouter une nouvelle citation")
    print("3. Lister toutes les citations")
    print("4. Quitter")
 
def main():
    citations = charger_citations()
    while True:
        afficher_menu()
        choix = input("Votre choix : ").strip()
        if choix == "1":
            print("\n...", random.choice(citations))
        elif choix == "2":
            nouvelle = input("Entrez votre citation : ").strip()
            if nouvelle:
                citations.append(nouvelle)
                sauvegarder_citations(citations)
                print("Citation ajoutée !")
        elif choix == "3":
            print("\n=== LISTE DES CITATIONS ===")
            for i, c in enumerate(citations, 1):
                print(f"{i}. {c}")
        elif choix == "4":
            print("À bientôt !")
            break
        else:
            print("Choix invalide.")
 
if __name__ == "__main__":
    main()
 

saving score / loading statistics ...