+ 1
Why does this code not work only in one of the test cases?
In the coding challenge "Longest Word" I made this code: txt = input() list = [] word_list = txt.split(" ") for word in word_list: list.append(word.index(word[-1])) print(word_list[list.index(max(list))]) Can someone tell me why this didn't work in only the second test case?
8 ответов
+ 6
You say index of s and gives you first s it finds which is at index 3
So your program thinks its 4 letter word
+ 4
a simple and pythonic way of getting the longest word of a string can be done like this:
inp = input().split()
print(max(inp, key = len)) # 'key = len' together with max() returns the longest word
+ 3
Expected output: perspiciatis (12 characters)
My output: accusantium (11 characters)
This is from python core.
+ 3
Thats because perspiciatis have two s
you don't get the last index work with length.
+ 2
Oh, I get it.
Thanks.
+ 2
You could append len(word) to your list instead of trying to deal with .index()
(And just an advice: avoid naming lists "list" – it might overwrite the build-in list())
+ 2
Right, I'll keep that in mind from now on.
Thank you!
+ 1
What did you get as output for the 2nd test case? Which Python course is it?