Advertisement
teslariu

ej10

Nov 2nd, 2021
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Ejercicio 10 del bonus track
  5.  
  6. Escribir un programa que cree un diccionario vacío y lo vaya llenando
  7. con personas. Donde el nombre(str) será la clave (el key) y el valor(value)
  8. la edad(int). El programa debe estar acompañado de un menú:
  9.  
  10. Menú:
  11. 1. Agregar
  12. 2. Mostrar el más chico
  13. 3. Mostrar el más grande
  14. 4. Salir
  15.  
  16.  
  17. La opción de agregar inserta a una persona. Mostrar el más chico, nos
  18. debería mostrar el nombre de la persona más chica, y viceversa el de
  19. mostrar el más grande. Con la opción 4 deberíamos salir del programa.
  20. Usar funciones para validar los ingresos.
  21.  
  22. personas = {"Juan":23, "Ana":11, "Tito":63, "Oscar":28}
  23.  
  24. """
  25. def ingresar_nombre():
  26.     while True:
  27.         nombre = input("Ingrese el nombre: ")
  28.         if not nombre.isspace() and len(nombre) > 0:
  29.             return nombre
  30.  
  31. def ingresar_edad():
  32.     while True:
  33.         edad = input("Ingrese la edad: ")
  34.         if edad.isdecimal() and int(edad) > 0:
  35.             return edad
  36.  
  37.  
  38.  
  39. personas = {}
  40.  
  41. while True:
  42.     print(
  43.     """
  44.         Menú:
  45.     1. Agregar
  46.     2. Mostrar el más chico
  47.     3. Mostrar el más grande
  48.     4. Salir
  49.     """)
  50.     opcion = input("Seleccione una opcion: ")
  51.    
  52.     if opcion == "1":
  53.         nombre = ingresar_nombre()
  54.         edad = ingresar_edad()
  55.         personas[nombre] = edad
  56.         edades = list(personas.values())
  57.         edades.sort()
  58.                
  59.        
  60.     elif opcion == "2":
  61.         for k,v in personas.items():
  62.             if v == edades[0]:
  63.                 nombre = k
  64.         print(f"El menor es {nombre} y tiene {edades[0]} años")       
  65.        
  66.  
  67.     elif opcion == "3":
  68.         for k,v in personas.items():
  69.             if v == edades[-1]:
  70.                 nombre = k
  71.         print(f"El mayor es {nombre} y tiene {edades[-1]} años")
  72.  
  73.  
  74.        
  75.     elif opcion == "4":
  76.         print("Hasta luego...")
  77.         break
  78.        
  79.     else:
  80.         print("Opción incorrecta...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement