Advertisement
teslariu

integrador con func

Feb 10th, 2022
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. TODO CON FUNCIONES
  6. CONSEJO:
  7. 1. cada opcion dentro del while True debería llamar a una funcion
  8. incluso el menu debe estar dentro de otra función
  9. 2. Las funciones con return
  10.  
  11. """
  12. ##### ACLARACION
  13.  
  14. """
  15. Una estructura real sería
  16. alumnos = {
  17.     33_333_333:{"nombre":"Juan", "total_cursos":2, "cursos":["Python", "Java"], "activo":True},
  18.     33_333_343:{"nombre":"Ana", "total_cursos":1, "cursos":["GO"]},
  19. }
  20.  
  21. """
  22.  
  23. ##### 1) Estructura de datos
  24.  
  25. # alumnos = {"Juan":3, "Ana":4, "Tito":5}
  26.  
  27. ###  2) Escribir el script
  28. # hay que hacer un menú con while True y opciones
  29.  
  30. # creo un diccionario vacío para agregarle los alumnos
  31.  
  32. ###### FUNCIONES  #######################
  33.  
  34. def menu():
  35.     return """
  36.     Menú de opciones
  37.     ----------------------
  38.     1. Ingresar alumno
  39.     2. Ver lista de alumnos
  40.     3. Ver cursos de un alumno
  41.     4. Salir
  42.     """
  43.    
  44. def ingresar_nombre(alumnos):
  45.     while True:
  46.         nombre = input("Ingrese el nombre: ")
  47.         if nombre and not nombre.isspace() and (nombre not in alumnos):
  48.             return nombre
  49.         else:
  50.             print("Error en el ingreso de nombre")
  51.  
  52.            
  53. def ingresar_cursos():
  54.     while True:
  55.         cursos = input("Ingrese la cantidad de cursos: ")
  56.         if cursos.isdecimal() and int(cursos):
  57.             return cursos
  58.         else:
  59.             print("Error en el ingreso de los cursos")
  60.  
  61.    
  62. def imprimir(dicc):
  63.     if dicc:
  64.         print("Lista de alumnos:")
  65.         for nombre,cursos in dicc.items():
  66.             print(f"{nombre} - {cursos} cursos")
  67.     else:
  68.         print("No existen alumnos")
  69.  
  70.  
  71. def ver_cursos():
  72.     # valido que el nombre no sea una cadena de espacios en blanco
  73.     while True:
  74.         nombre = input("Ingrese el nombre: ")
  75.         if nombre and not nombre.isspace():
  76.             break
  77.         else:
  78.             print("Error en el ingreso de nombre")
  79.        
  80.     # compruebo si el nombre del alumno existe
  81.     if nombre in alumnos:
  82.         return f"{nombre} - {alumnos[nombre]} cursos"
  83.    
  84.     else:
  85.         return f"No existe el alumno {nombre}"
  86.    
  87.  
  88.  
  89.  
  90. alumnos = {}
  91.  
  92. print("\nPrograma de administración de alumnos")
  93. print("---------------------------------------")
  94.  
  95. while True:
  96.     print(menu())
  97.     opcion = input("Seleccione una opción: ")
  98.    
  99.     if opcion == "1":
  100.         nombre = ingresar_nombre(alumnos)
  101.         cursos = ingresar_cursos()
  102.         alumnos[nombre] = cursos
  103.            
  104.        
  105.     elif opcion == "2":
  106.         imprimir(alumnos)
  107.        
  108.        
  109.     elif opcion == "3":
  110.         print(ver_cursos())
  111.        
  112.        
  113.     elif opcion == "4":
  114.         print("Gracias por usar este script...")
  115.         break
  116.    
  117.        
  118.     else:
  119.         print("Opción inválida")
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement