I was trying a leetcode, where we have to calculate the number of words in a sentence. The code I wrote calculates the number of words but for some reason it just appends 1 to the max_words list each time, regardless of the number of words.
def mostWordsFound(sentences):
max_words=[]
for i in range(0,len(sentences)):
num_of_words=1
for j in range(0,len(sentences[i])):
if sentences[i][j]==" ":
num_of_words+=1
print(num_of_words)
if j==len(sentences[i][j])-1:
print("reached here")
max_words.append(num_of_words)
j+=1
i+=1
print(max_words)
sentences = ["please wait.", "continue to fight.", "continue to win."]
mostWordsFound(sentences)