I have following function in Python which should iterate over all text and return the words from index 0 to 5, from 1 to 6, from 2 to 7 and so on till the end of text. My function works well with print()
statement, but when I use return
instead of it, it works only till [0:5]
then stops. Which keyword should I use without breaking loop ?
f1 = open("doc1.txt", 'r')
f1_words = f1.read().lower().split()
def iterate():
for i in range(len(f1_words) - 4):
for j in range(len(f1_words)+1):
if(j-i==5):
words1 = f1_words[i:j]
return words1
else:
print(i, j)
print(iterate())