I've figured out the error UnboundError : The loop has to finish before I can return any value return function has to be placed outside of the loop somehow