Advertisement
LinuxAIO

Directorio_original

Aug 12th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | None | 0 0
  1. agenda = []
  2. cambio = False
  3. uso = " Defecto "
  4. def pide_nombre(comp=""):
  5.     nombre = input("Nombre: ")
  6.     if nombre == "":
  7.         nombre = comp
  8.     return(nombre)
  9. def pide_telefono(comp=""):
  10.     telefono = input("Telefono: ")
  11.     if telefono == "":
  12.         telefono = comp
  13.     return(telefono)
  14. def pide_cedula():
  15.     return(int(input("Ingrese su cedula: ")))
  16. def muestra_datos(nombre, telefono, cedula):#Escribir los archivos
  17.     print("Nombre: {} Telefono: {} Cedula: {}".format(nombre,telefono,cedula))
  18. def pide_nombre_archivo():#Nombre achivo -> Guardar Informacion
  19.     return(input("Nombre del archivo: "))
  20. def investigacion(nombre):
  21.     mnombre = nombre.lower()
  22.     for p, e in enumerate(agenda):
  23.         if e[0].lower() == mnombre:#Campo Nombre
  24.             return p
  25.     return None
  26. def nuevo():
  27.     global agenda, cambio
  28.     nombre = pide_nombre()
  29.     if investigacion(nombre) != None:
  30.         print("Lo sentimos el nombre existe")
  31.         return
  32.     telefono = pide_telefono()
  33.     cedula = pide_cedula()
  34.     agenda.append([nombre, telefono, cedula])
  35.     agenda.sort()
  36.     cambio = True
  37. def borra():
  38.     global agenda, cambio
  39.     nombre = pide_nombre()
  40.     p = investigacion(nombre)
  41.     if p != None:
  42.         x = comprobar()
  43.         if x:
  44.             del agenda[p]
  45.             cambio = True
  46.     else:
  47.         print("Nombre no encontrado.")
  48. def comprobar():
  49.     n = int(input("Desea realizar esta operacion? 1: Si, 2: No -> "))
  50.     if n == 1:
  51.         return True
  52. def altera():
  53.     global cambio
  54.     p = investigacion(pide_nombre())
  55.     if p != None:
  56.         nombre = agenda[p][0]
  57.         telefono = agenda[p][1]
  58.         cedula = agenda[p][2]
  59.         print("Encontrado: ")
  60.         muestra_datos(nombre, telefono, cedula)
  61.         x = comprobar()
  62.         if (x):
  63.             nombre = pide_nombre()
  64.             telefono = pide_telefono()
  65.             cedula = pide_cedula()
  66.             agenda[p] = [nombre, telefono, cedula]
  67.             cambio = True
  68.     else:
  69.         print("Nombre no encontrado.")
  70. def lista():
  71.     print("\nAgenda\n\n------")
  72.     x = 1
  73.     for e in agenda:
  74.         print("%d)" % x, end="")
  75.         muestra_datos(e[0], e[1], e[2]) #Nombre Telefono Cedula
  76.         print("-------\n")
  77.         x = x+1
  78. def lee():
  79.     global agenda, cambio, uso
  80.     if (cambio):
  81.         print("No has guardado los cambios en el archivo, desea guardar?")
  82.         x = comprobar()
  83.         if(x):
  84.             graba()
  85.     nombre_archivo = pide_nombre_archivo()
  86.     uso = nombre_archivo
  87.     archivo = open(nombre_archivo, "r", encoding="utf-8")
  88.     agenda = []
  89.     for l in archivo.readlines(): # JONATHAN 5772359 25211873 Jesus#
  90.         nombre, telefono, cedula = l.strip().split(" ")
  91.         agenda.append([nombre, telefono, cedula]) #Numero -> Caracteres
  92.     agenda.sort() #Aqui aplica
  93.     cambio = False
  94.     archivo.close()
  95.    
  96.    
  97. def graba():
  98.     global uso
  99.     nombre_archivo = pide_nombre_archivo()
  100.     uso = nombre_archivo
  101.     archivo = open(nombre_archivo, "w", encoding="utf-8")
  102.     for e in agenda:
  103.         archivo.write("{} {} {}\n".format(e[0],e[1],e[2]))
  104.     archivo.close()
  105.                      
  106. def valida_franja_entero(pregunta, inicio, fin):
  107.     while True:        
  108.         try:
  109.             valor = int(input(pregunta))
  110.             if inicio <= valor <= fin:
  111.                 return(valor)
  112.         except ValueError:
  113.             print("Valor invalido, por favor digitar entre {} y {}".format(inicio,fin))
  114. def menú():
  115.     print("""
  116. 1 - Nuevo
  117. 2 - Altera
  118. 3 - Borra
  119. 4 - Lista
  120. 5 - Graba
  121. 6 - Lee
  122. 0 - Salir
  123. """)
  124.     print("Archivo utilizando actualmente: %s" % uso)
  125.     print("Tamaño de la agenda: %d Dato Cambiado: %s" % (len(agenda), cambio))
  126.     return valida_franja_entero("Elección una opción: ",0,6)
  127.  
  128. while True:
  129.     opción = menú()
  130.     if opción == 0:
  131.         break
  132.     elif opción == 1:
  133.         nuevo()
  134.     elif opción == 2:
  135.         altera()
  136.     elif opción == 3:
  137.         borra()
  138.     elif opción == 4:
  139.         lista()
  140.     elif opción == 5:
  141.         graba()
  142.     elif opción == 6:
  143.         lee()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement