-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact_mgmt.py
More file actions
78 lines (59 loc) · 2.04 KB
/
contact_mgmt.py
File metadata and controls
78 lines (59 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
FILENAME = "contacts.txt"
# 1. Afficher les contacts qui se trouvent dans le fichier
def afficher_contacts(contacts):
if not contacts:
print("Auncun contact.")
return
print(f"\nVos {len(contacts)} contacts :")
for i, (nom, telephone) in enumerate(contacts, 1):
print(f"{i}. {nom} - {telephone}")
def charger_contacts():
contacts = []
try:
with open(FILENAME, "r", encoding="utf-8") as fichier:
for ligne in fichier:
ligne = ligne.strip()
if ligne:
nom, telephone = ligne.split(";")
contacts.append([nom, telephone])
print(f"\n{len(contacts)} contact(s) chargés")
except FileNotFoundError:
print("Nouveau fichier de contacts")
return contacts
# 2. Créer des contacts
def ajouter_contact(contacts):
print("\nAjouter un contact :")
nom = input("Nom : ").strip()
telephone = input("Téléphone : ").strip()
if nom and telephone:
contacts.append([nom, telephone])
print(f"{nom} ajouté !")
else:
print("Nom et téléphone obligatoires")
# 3. Sauver les contacts et quitter le programme
def sauvegarder_contacts(contacts):
with open(FILENAME, "w", encoding="utf-8") as fichier:
for nom, telephone in contacts:
fichier.write(f"{nom};{telephone}\n")
print(f"{len(contacts)} contacts enregistrés")
def menu():
print("=== GESTIONNAIRE DE CONTACTS ===")
contacts = charger_contacts()
while True:
print("\n Que voulez-vous faire ?")
print("1. Voir les contacts")
print("2. Ajouter un contact")
print("3. Sauver et quitter")
choix = input("Votre choix : ")
if choix == "1":
afficher_contacts(contacts)
elif choix == "2":
ajouter_contact(contacts)
elif choix == "3":
sauvegarder_contacts(contacts)
print("Au revoir !")
break
else:
print("Choix invalide")
if __name__ == "__main__":
menu()