Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

Python Error 'NoneType' object cannot be interpreted as an integer issue?

Writer Olivia Zamora

I am trying to run the following piece of code but I always get that error,Python Error 'NoneType' object cannot be interpreted as an integer, any help?

 sent_maxlen= None
def get_fixed_size(sents): """ Partition sents into lists of sent_maxlen elements (execept the last in each sentence, which might be shorter) """ return [sent[s_ind : s_ind + sent_maxlen] for sent in sents for s_ind in range(0, len(sent), sent_maxlen)] 
3

1 Answer

I think there are some typos in your code. There is both sent_maxlex, and sent_maxlen.

In the code you posted, sent_maxlen (assuming the above typo was unintentional), is initialised to be None. This variable is used as the third argument in the range() function, which is the 'step' or the 'stride' of the iterator, that is, how much the iterator is incremented by each iteration (so it makes sense it cannot be non-numeric, and should have an absolute value greater than zero if we want the loop to ever finish).

From the function description it looks like the function is meant to turn a list of words into a list of lists of words, which each sublist having sent_maxlen elements (apart from the last sublist). As the user of the code, can you correctly initialise the sent_maxlen to what length you want these sublists to be?

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy