why does it give an indexerror | Sololearn: Learn to code for FREE!


why does it give an indexerror


4/27/2021 8:54:00 AM

Майкл Дориан

7 Answers

New Answer


Майкл Дориан To print the bigger word, put this in the body of the while while c < len(a): b = len(a[c]) if b > n: n = b cm = c c += 1


txt = "Sololearn is cool platform" words = txt.split() for a in words: print(a) Easy way to access each word from the list of words. --------------------------------------------------------------------- [Longest word in the list of words] txt = input() or "Sololearn is cool" words = txt.split() longest = "" for word in words: if len(word) > len(longest): longest = word print("longest word is", longest, "with length of", len(longest))


c<len(a) instead of c<=len(a). if you do c<=len(a) you are checking for value at index 2 as well if for example the input is "hello world".


Майкл Дориан replace while c <= len(a): with while c < len(a):


because the index is out of range. what are you trying to do?


I want to get the longest word in a sentence