0

I have a function calc_factors which is supposed to return a list of list, but for some reason is returning None. When I run my code using breakpoints the value I am trying to return isn't None:image showing value for returning variable is not None, i.e. usable_factor_pairs has a value of [[7,8]].

However, When I continue executing the code, the function seems to return nothing to the variable 'r', as evidenced by enter image description here, and as a result, during the function call it says that the 'NoneType' object is not subscriptable.

Is there a reason why usable_factor_pairs is being returned as None?

Here's the full code below:

import numpy as np
def cipher_text(plain_text):
    normalized_text = [x.lower() for x in plain_text if x.isalpha()]

    def calc_factors(x):   
        factor_values = [i for i in range(1, x+1) if x % i == 0]
        factor_pairs = [[v, int(x/v)] for v in factor_values]
        for p in factor_pairs:
            if p[::-1] in factor_pairs:
                factor_pairs.remove(p)
        usable_factor_pairs = [p for p in factor_pairs if p[1] >= p[0] and (p[1] - p[0] <= 1)]
        if len(usable_factor_pairs) > 1:
            raise ValueError("Text provided cannot be calculated")
        elif not usable_factor_pairs:
            calc_factors(x+1)
        else:
            return usable_factor_pairs


    r = calc_factors(len(normalized_text))[0][0]
    c = calc_factors(len(normalized_text))[0][1]
    pad_amt = len(normalized_text) -  (r * c)
    pad_lst = pad_amt * [' ']
    padded_str = normalized_text + pad_lst
    new_str = ''.join(padded_str)
    result = np.empty([r,c],dtype = str)
    k = 0
    for j in range(c):
        for i in range(r):
            result[i][j] = new_str[k]
            k+=1
    flat_list = [item for sublist in result.tolist() for item in sublist]

    if r > 1:
        i = c
        while i < len(flat_list):
            flat_list.insert(i, ' ')
            i += (c+1)

    final = ''.join(flat_list)
    return final



test = cipher_text('If man was meant to stay on the ground, '
                        'god would have given us roots.')
print(test)

Thanks in advance.

0 Answers0