Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- TODO CON FUNCIONES
- CONSEJO:
- 1. cada opcion dentro del while True debería llamar a una funcion
- incluso el menu debe estar dentro de otra función
- 2. Las funciones con return
- """
- ##### ACLARACION
- """
- Una estructura real sería
- alumnos = {
- 33_333_333:{"nombre":"Juan", "total_cursos":2, "cursos":["Python", "Java"], "activo":True},
- 33_333_343:{"nombre":"Ana", "total_cursos":1, "cursos":["GO"]},
- }
- """
- ##### 1) Estructura de datos
- # alumnos = {"Juan":3, "Ana":4, "Tito":5}
- ### 2) Escribir el script
- # hay que hacer un menú con while True y opciones
- # creo un diccionario vacío para agregarle los alumnos
- ###### FUNCIONES #######################
- def menu():
- return """
- Menú de opciones
- ----------------------
- 1. Ingresar alumno
- 2. Ver lista de alumnos
- 3. Ver cursos de un alumno
- 4. Salir
- """
- def ingresar_nombre(alumnos):
- while True:
- nombre = input("Ingrese el nombre: ")
- if nombre and not nombre.isspace() and (nombre not in alumnos):
- return nombre
- else:
- print("Error en el ingreso de nombre")
- def ingresar_cursos():
- while True:
- cursos = input("Ingrese la cantidad de cursos: ")
- if cursos.isdecimal() and int(cursos):
- return cursos
- else:
- print("Error en el ingreso de los cursos")
- def imprimir(dicc):
- if dicc:
- print("Lista de alumnos:")
- for nombre,cursos in dicc.items():
- print(f"{nombre} - {cursos} cursos")
- else:
- print("No existen alumnos")
- def ver_cursos():
- # valido que el nombre no sea una cadena de espacios en blanco
- while True:
- nombre = input("Ingrese el nombre: ")
- if nombre and not nombre.isspace():
- break
- else:
- print("Error en el ingreso de nombre")
- # compruebo si el nombre del alumno existe
- if nombre in alumnos:
- return f"{nombre} - {alumnos[nombre]} cursos"
- else:
- return f"No existe el alumno {nombre}"
- alumnos = {}
- print("\nPrograma de administración de alumnos")
- print("---------------------------------------")
- while True:
- print(menu())
- opcion = input("Seleccione una opción: ")
- if opcion == "1":
- nombre = ingresar_nombre(alumnos)
- cursos = ingresar_cursos()
- alumnos[nombre] = cursos
- elif opcion == "2":
- imprimir(alumnos)
- elif opcion == "3":
- print(ver_cursos())
- elif opcion == "4":
- print("Gracias por usar este script...")
- break
- else:
- print("Opción inválida")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement