Advertisement
LinuxAIO

Directorio2

Aug 12th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 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):#Escribir los archivos
  17.     print("Nombre: {} Telefono: {}".format(nombre,telefono))
  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:
  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.         print("Encontrado: ")
  59.         muestra_datos(nombre, telefono)
  60.         x = comprobar()
  61.         if (x):
  62.             nombre = pide_nombre()
  63.             telefono = pide_telefono()
  64.             agenda[p] = [nombre, telefono]
  65.             cambio = True
  66.     else:
  67.         print("Nombre no encontrado.")
  68. def lista():
  69.     print("\nAgenda\n\n------")
  70.     x = 1
  71.     for e in agenda:
  72.         print("%d)" % x, end="")
  73.         muestra_datos(e[0], e[1])
  74.         print("-------\n")
  75.         x = x+1
  76. def lee():
  77.     global agenda, cambio, uso
  78.     if (cambio):
  79.         print("No has guardado los cambios en el archivo, desea guardar?")
  80.         x = comprobar()
  81.         if(x):
  82.             graba()
  83.     nombre_archivo = pide_nombre_archivo()
  84.     uso = nombre_archivo
  85.     archivo = open(nombre_archivo, "r", encoding="utf-8")
  86.     agenda = []
  87.     for l in archivo.readlines(): #JONATHAN#5772359 Jesus#
  88.         nombre, telefono = l.strip().split(" ")
  89.         agenda.append([nombre, telefono]) #Numero -> Caracteres
  90.     agenda.sort() #Aqui aplica
  91.     cambio = False
  92.     archivo.close()
  93.    
  94.    
  95. def graba():
  96.     global uso
  97.     nombre_archivo = pide_nombre_archivo()
  98.     uso = nombre_archivo
  99.     archivo = open(nombre_archivo, "w", encoding="utf-8")
  100.     for e in agenda:
  101.         archivo.write("{} {}\n".format(e[0],e[1]))
  102.     archivo.close()
  103.                      
  104. def valida_franja_entero(pregunta, inicio, fin):
  105.     while True:        
  106.         try:
  107.             valor = int(input(pregunta))
  108.             if inicio <= valor <= fin:
  109.                 return(valor)
  110.         except ValueError:
  111.             print("Valor invalido, por favor digitar entre {} y {}".format(inicio,fin))
  112. def menú():
  113.     print("""
  114. 1 - Nuevo
  115. 2 - Altera
  116. 3 - Borra
  117. 4 - Lista
  118. 5 - Graba
  119. 6 - Lee
  120. 0 - Salir
  121. """)
  122.     print("Archivo utilizando actualmente: %s" % uso)
  123.     print("Tamaño de la agenda: %d Dato Cambiado: %s" % (len(agenda), cambio))
  124.     return valida_franja_entero("Elección una opción: ",0,6)
  125.  
  126. while True:
  127.     opción = menú()
  128.     if opción == 0:
  129.         break
  130.     elif opción == 1:
  131.         nuevo()
  132.     elif opción == 2:
  133.         altera()
  134.     elif opción == 3:
  135.         borra()
  136.     elif opción == 4:
  137.         lista()
  138.     elif opción == 5:
  139.         graba()
  140.     elif opción == 6:
  141.         lee()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement