Advertisement
teslariu

integrador con funciones

Feb 4th, 2022
1,530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Mejorando todo con funciones
  5. """
  6.  
  7. # Diseño mi estructura de datos:
  8. # alumnos = {"Pepe": 8, "Juana": 3, .........., "Vicky": 3}
  9.  
  10. # Como necesito un diccionario para los alumnos, creo uno vacía
  11. alumnos = {}
  12.  
  13. # Creo un template while True con un menu de opciones
  14. print("Menú de administración de alumnos de Educación IT")
  15.  
  16.  
  17. ##################   Funciones   ###################################
  18.  
  19. def imprimir_menu():
  20.     print("""
  21.     1 - Ver la lista de alumnos.
  22.     2 - Añadir un alumno a la lista.
  23.     3 - Ver la cantidad de cursos.
  24.     4 - Salir.
  25.     """)
  26.    
  27.    
  28. def imprimir_lista(alumnos):
  29.     if alumnos:
  30.         print("Lista de alumnos:")
  31.         for nombre, cursos in alumnos.items():
  32.             print(f"{nombre} - {cursos} cursos")
  33.     else:
  34.         print("No hay alumnos inscriptos")
  35.        
  36.  
  37.  
  38. def validar_cursos():
  39.     while True:
  40.         cursos = input("Ingrese la cantidad de cursos: ")
  41.         if cursos.isdecimal() and cursos != "0":
  42.             return int(cursos)
  43.         else:
  44.             print("Error: debe ingresar un entero mayor a cero")
  45.  
  46.            
  47.  
  48. def validar_alumnos(longitud_lista):
  49.     if len(alumnos) == longitud_lista + 1:
  50.         return "¡El alumno fue añadido a la lista!"
  51.     else:
  52.         return "Error, no se ha añadido el alumno"
  53.    
  54.  
  55.  
  56. def validar_nombre():
  57.     while True:
  58.         nombre = input("Ingrese el nombre del alumno: ")
  59.         if not nombre.isspace() and nombre:
  60.             return nombre
  61.         else:
  62.             print("Error: debe ingresar un nombre")
  63.  
  64.        
  65.    
  66. def ver_cursos(alumnos):
  67.     if alumnos:
  68.         nombre = input('Ingrese el nombre del alumno que busca: ')
  69.         if nombre in list(alumnos.keys()):
  70.             return f'El alumno {nombre} posee {alumnos[nombre]} cursos'
  71.         else:
  72.             return f'El alumno {nombre} no esta en la Lista'
  73.     else:
  74.         return "No hay alumnos inscriptos aún"
  75.    
  76.    
  77.    
  78.  
  79.  
  80. ####################################################################
  81.  
  82.  
  83. while True:
  84.     imprimir_menu()
  85.    
  86.     opcion = input("Seleccione una opción: ")
  87.    
  88.     if opcion == "1":
  89.         imprimir_lista(alumnos)
  90.        
  91.     elif opcion == "2":
  92.         longitud_lista = len(alumnos)
  93.         nombre = validar_nombre()
  94.         cursos = validar_cursos()
  95.         alumnos[nombre] = cursos
  96.         print(validar_alumnos(longitud_lista))
  97.                
  98.        
  99.     elif opcion == "3":
  100.         print(ver_cursos(alumnos))
  101.        
  102.            
  103.     elif opcion == "4":
  104.         print("Gracias por utilizar este script...")
  105.         break
  106.        
  107.     else:
  108.         print("Opción incorrecta")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement