I want to create a list and output the longest word which is rockey but I'm getting town as my output. What's with my code. Py 3 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I want to create a list and output the longest word which is rockey but I'm getting town as my output. What's with my code. Py 3

myName = [] name = "owusu rockey is the new king in town" new = name.split() myName.append(new) for word in myName: print(max(word))

10th Dec 2020, 10:02 AM
Augustine Ansah Owusu
Augustine Ansah Owusu - avatar
3 Answers
+ 5
name = "owusu rockey is the new king in town" print(max(name.split(), key=len)) Read-up on the built-ins here:- https://docs.python.org/3/library/functions.html#built-in-funcs
10th Dec 2020, 10:22 AM
rodwynnejones
rodwynnejones - avatar
+ 3
name = "owusu rockey is the new king in town" words = name.split() maxLen = len(words[0]) #is a max lenght of the word maxInx = 0 #is a index of the biggest word for i in range(len(words)): word = words[i] if len(word) > maxLen: maxInx = i maxLen = len(word) print(words[maxInx])
10th Dec 2020, 10:13 AM
Иван Чикyнов
Иван Чикyнов - avatar
+ 2
you probablly printed the max index word. you may need to check the len() of each word and compare them, then print whatever one has the same amount
10th Dec 2020, 10:11 AM
Slick
Slick - avatar