-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagenda.py
More file actions
108 lines (97 loc) · 2.9 KB
/
agenda.py
File metadata and controls
108 lines (97 loc) · 2.9 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# agenda.py
#
# Copyright 2017 yorlys <yorlys@debian>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
from record import record
class Entrada(record):
nombre = ''
apellido = ''
telefono = ''
def lee_entrada():
nombre = raw_input('Nombre: ')
apellido = raw_input('Apellido: ');
telefono = raw_input('Telefono: ')
return Entrada(nombre=nombre, apellido=apellido, telefono=telefono)
class Agenda(record):
lista = []
def cargar_agenda(agenda):
agenda.lista = []
f = open('agenda.txt', 'r')
while 1:
linea1 = f.readline()
linea2 = f.readline()
linea3 = f.readline()
if linea1 == '':
break
entrada = Entrada(nombre=linea1[:-1], apellido=linea2[:-1], telefono=linea3[:-1])
agenda.lista.append(entrada)
f.close()
def guardar_agenda(agenda):
f = open('agenda.txt', 'w')
for entrada in agenda.lista:
f.write(entrada.nombre + '\n')
f.write(entrada.apellido + '\n')
f.write(entrada.telefono + '\n')
f.close()
def buscar_telefono(agenda, nombre, apellido):
for entrada in agenda.lista:
if entrada.nombre == nombre and entrada.apellido == apellido:
return entrada.telefono
return ''
def anyadir_entrada(agenda, entrada):
agenda.lista.append(entrada)
def borrar_entrada(agenda, nombre, apellido):
for i in range(len(agenda.lista)):
if agenda.lista[i].nombre == nombre and agenda.lista[i].apellido == apellido:
del agenda.lista[i]
return
def menu():
print '1) Añadir entrada'
print '2) Consultar agenda'
print '3) Borrar entrada'
print '4) Salir'
opcion = int(raw_input('Seleccione opcion: '))
while opcion < 1 or opcion > 4:
opcion = int(raw_input('Seleccione opcion(entre 1 y 4): '))
return opcion
agenda = Agenda()
cargar_agenda(agenda)
opcion = menu()
while opcion != 4:
if opcion == 1:
entrada = lee_entrada()
anyadir_entrada(agenda, entrada)
elif opcion == 2:
nombre = raw_input('Nombre')
apellido = raw_input('Apellido: ')
telefono = buscar_telefono(agenda
,nombre, apellido)
if telefono == '':
print 'No esta en la agenda'
else:
print 'Telefono: ', telefono
elif opcion == 3:
nombre = raw_input('Nombre: ')
apellido = raw_input('Apellido: ')
borrar_entrada(agenda, nombre, apellido)
opcion = menu()
guardar_agenda(agenda)