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)
10 ответов
+ 20
txt = input()
longest = max(txt.split(), key = len)
print(longest)
+ 11
Yeah why key=len part?
+ 6
This will help you:
https://code.sololearn.com/cd89N09f7P0R/?ref=app
+ 6
Why did you write 'key=len'? What does it mean? Thanks so much guys!!
+ 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)
+ 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])
+ 2
you need to convert the input into a list first.
txt = input().split()
+ 1
I think key=len tells the program to sort the list according to the length of the elements
+ 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))
- 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)