Longest Word problem Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Longest Word problem Python

What am I doing wrong with my code here? I am trying to solve the longest word challenge. It isn’t finding the word. Thanks! txt = input() def find_longest_word (txt): longest_word = max(txt, key=len) return longest_word print(txt)

29th Dec 2020, 9:05 PM
CmplXty
10 Answers
+ 20
txt = input() longest = max(txt.split(), key = len) print(longest)
30th Dec 2020, 3:44 AM
K.S.S. Karunarathne
K.S.S. Karunarathne - avatar
+ 11
Yeah why key=len part?
5th Apr 2021, 1:34 PM
Reshawn
Reshawn - avatar
29th Dec 2020, 9:25 PM
JaScript
JaScript - avatar
+ 6
Why did you write 'key=len'? What does it mean? Thanks so much guys!!
16th Feb 2021, 5:29 PM
Ivan Voncini
Ivan Voncini - avatar
+ 4
#welcome to my version))) text = input() words = text.split(" ") max = "" for i in range(len(words)): if len(words[i]) > len(max): max=words[i] print(max)
16th Aug 2021, 8:19 PM
Hamdambek🖥️🎧
Hamdambek🖥️🎧 - avatar
+ 3
txt = input().split() #your code goes here word = [word for word in txt] letter = [len(letter) for letter in word] place = letter.index(max(letter)) print(txt[place])
26th Jan 2021, 10:55 PM
Ali Kelidari
Ali Kelidari - avatar
+ 2
you need to convert the input into a list first. txt = input().split()
29th Dec 2020, 9:21 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
I think key=len tells the program to sort the list according to the length of the elements
14th Jan 2022, 8:05 PM
Brooke White
Brooke White - avatar
+ 1
txt = input() txt_list = txt.split() #why key=len def find_by_value(v): for key, value in dictionary.items(): if v == value: return key #the code for below to make dictionary not empty for add in txt_list: dictionary[f"{add}"]=len(add) dictionary={} finding_max_length=max(dictionary.values()) print(find_by_value(o))
20th Apr 2022, 8:10 PM
Volvo4545
Volvo4545 - avatar
- 1
#My version:)) txt = input() a = txt.split() d = [] for item in a: d = d + [len(item)] #This gives me a list 'd' with all character lengths e = max(d) #Now let's find the word associated with the maximum length for word in a: if len(word) == e: print(word)
9th Dec 2022, 6:07 PM
Aaditya Singh
Aaditya Singh - avatar