+ 1

Find Longest Word Python

Please help. This is my code: txt = input() def longword(): x = max(txt.split()) return x print(longword()) Now, I know the only thing I am missing is key=len right after the txt.split(), but I don't know why this is required. I thought the max function just returned the longest word in a list. Why do I need to add this to make it work? Because without it, every test fails.

24th Sep 2021, 8:05 PM
ozzy
ozzy - avatar
2 Respostas
+ 5
Because otherwise, max() compares words based on the first letter (a<b<c...). and if they are equal, it checks the next letters.
24th Sep 2021, 8:18 PM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
+ 2
Ok, thank you. That makes much more sense now.
24th Sep 2021, 8:21 PM
ozzy
ozzy - avatar