teslariu

agenda con diccionarios

Jul 8th, 2023
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Script que implementa una agenda telefónica. Usaremos diccionarios para
  5. estructurar los datos de la siguiente manera
  6. EJ:
  7. agenda = {"Juan":25252, "Jorge":3132}
  8. (suponemos un solo teléfono por persona)
  9. """
  10. agenda = {}
  11.  
  12. menu = """
  13.    Agenda telefónica
  14.    --------------------
  15.    1. Agregar contacto
  16.    2. Modificar número  Tito 300
  17.    3. Modificar nombre  Juan 200
  18.    -------------------
  19.    4. Consultar número
  20.    5. Consultar nombre
  21.    6. Imprimir agenda
  22.    -------------------
  23.    7. Eliminar contacto
  24.    8. Salir
  25.    --------------------
  26. """
  27.  
  28. while True:
  29.     print(menu)
  30.     opcion = input("Seleccione una opción: ")
  31.    
  32.     if opcion == "1":
  33.         nombre = input("Ingrese el nombre: ")
  34.         numero = input("Ingrese el número de teléfono: ")
  35.         agenda[nombre] = numero
  36.         print("Contacto guardado")
  37.        
  38.        
  39.     elif opcion == "2":
  40.         nombre = input("Ingrese el nombre: ")
  41.         lista_nombres = agenda.keys()
  42.         if nombre in lista_nombres:
  43.             numero = input("Ingrese el nuevo número de teléfono: ")
  44.             agenda[nombre] = numero
  45.             print("Contacto modificado")
  46.         else:
  47.             print("Contacto no hallado")
  48.            
  49.  
  50.     elif opcion == "3":
  51.         numero = input("Ingrese el numero: ")
  52.         hallado = False
  53.         for k,v in agenda.items():
  54.             if v == numero:
  55.                 del(agenda[k])   # borro el contacto viejo
  56.                 nombre = input("Ingrese el nuevo nombre: ")
  57.                 agenda[nombre] = numero
  58.                 print("Contacto modificado")
  59.                 hallado = True
  60.                 break
  61.         if not hallado:
  62.             print("No existe el contacto")
  63.    
  64.        
  65.     elif opcion == "4":
  66.         numero = input("Ingrese el numero: ")
  67.         hallado = False
  68.         for k,v in agenda.items():
  69.             if v == numero:
  70.                 print(f"Nombre: {k}")
  71.                 hallado = True
  72.                 break
  73.         if not hallado:
  74.             print("No existe el contacto")
  75.        
  76.            
  77.        
  78.     elif opcion == "5":
  79.         nombre = input("Ingrese el nombre: ")
  80.         lista_nombres = agenda.keys()
  81.         if nombre in lista_nombres:
  82.             print(f"Teléfono: {agenda.get(nombre)}")
  83.         else:
  84.             print("Contacto no hallado")
  85.  
  86.        
  87.     elif opcion == "6":
  88.         if agenda:
  89.             for k,v in agenda.items():
  90.                 print(f"Nombre: {k} - teléfono: {v}")
  91.         else:
  92.             print("No existen contactos")
  93.  
  94.  
  95.     elif opcion == "7":
  96.         nombre = input("Ingrese el nombre: ")
  97.         lista_nombres = agenda.keys()
  98.         if nombre in lista_nombres:
  99.             del(agenda[nombre])
  100.             print("Contacto eliminado")
  101.         else:
  102.             print("Contacto no hallado")
  103.        
  104.        
  105.     elif opcion == "8":
  106.         print("Gracias por utlizar este programa...")
  107.         break
  108.        
  109.     else:
  110.         print("Opción incorrrecta")
  111.  
Add Comment
Please, Sign In to add comment