+ 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.
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.
+ 2
Ok, thank you. That makes much more sense now.