+ 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))
3 odpowiedzi
+ 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
+ 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])
+ 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