+ 1

Help me. I tried this code for longest word in python. I even succeeded in the first 3 tests but failed in the last one.

txt = input() data = txt.split(" ") for x in data : a = str(len(x)) if str(len(x)) != str(max(list(a))) : print(x) break Pls help me to solve.

10th May 2021, 12:13 PM
Ritesh Behera
Ritesh Behera - avatar
3 Réponses
+ 2
Here's a possible solution: a = input().split() b = max([len(x) for x in a]) [print(x) for x in a if len(x) == b] # Hope this helps
10th May 2021, 12:24 PM
Calvin Thomas
Calvin Thomas - avatar
+ 7
Ritesh Behera , ▪︎here is an other possible solution. it splits the input to a list, then these list is sorted with length of the words as key: res = input().split() print(sorted(res,key=len)[-1]) ▪︎an other (more simple) solution by using max could be: a = input().split() print(max(a, key = len)) max has an optional parameter "key" that can be used.
10th May 2021, 2:15 PM
Lothar
Lothar - avatar
+ 1
Thnx broo
10th May 2021, 1:30 PM
Ritesh Behera
Ritesh Behera - avatar