+ 4
This code is supposed to find the longest word in a string using the split function but it doesn't work with me HELP !
txt = input() txt.split() txt[0]=max for i in len(txt): if len(txt[i])>max: max=txt[i] print(max)
59 Respuestas
+ 20
txt = input()
txt = txt.split() # Reassigning txt because split does
not modify the original string
max = len(txt[0])
longest = ''
for i in range(len(txt)):
if len(txt[i]) > max:
max = len(txt[i])
longest = txt[i]
print(longest)
+ 11
By default, max() returns a value which has higher ASCII value.
You can use key (optional argument of max() )to compare the strings by their length.
print(max(txt.split(), key = len))
+ 7
user sentence = input()
longest = max(sentence.split(), key=len)
print(longest)
+ 5
This one hurt my confidence. I dont think we have learned the easier key=len thing either.
txt = input()
lst = txt.split(" ")
long = 0
for w in lst:
length=len(w)
if length > long:
long = length
for word in lst:
if len(word) == long:
print(word)
+ 4
In your code what is max? Is it length of word or str the word itself? If you write txt[0] = max and then use if statement like if len(txt[i])>max This make no sense.
+ 4
Here is a definitive working version of your code. There were a couple tweaks beyond my listed changes. Usually I am more comprehensive, but I am healing from illness right now, so I apologize.
txt = input()
txt = txt.split()
max=len(txt[0])
for i in txt:
if len(i)>max:
max=len(i)
print(max)
+ 4
Travis Beard it looks like after you save the length if the largest word, you loop over the original array again, and print every word equal to that max length. If multiple words are just as long, they also get printed again :)
+ 3
Tjipke van der Heide in your last code, the print may output words that are less than max length, until that absolute max length is found.
Edit: I see that is corrected now.
+ 3
Brian only for a second ^^ I edited it quickly after posting :)
+ 3
Wijdene Madiouni is because of speed? Hybridize it with Simba's suggestion that cuts out looping.
Or is there an unmentioned other requirement, like filtering punctuation so it is not part of word length?
+ 3
Travis Beard
txt = input()
lst = txt.split(" ")
long = 0
#here u loop over lst
for w in lst:
length=len(w)
if length > long:
#if this word is bigger, update long
long = length
#this is a new loop over lst
for word in lst:
#if word is as long as long
if len(word) == long:
#print it
print(word)
Every word as long as long is printed here :) an alternative solution could be that, just like in Wijdene Madiouni 's code, you just save the longest word in the first loop, and just print that when the loops over. Then there's no need for a second loop, making it shorter and easier to understand :)
+ 3
print(max({i:len(i) for i in input().split()}))
This also returns longest word.
Use this one line code.
+ 2
Try this Example hope it work
txt = input("Enter Text:").split()
longest = ""
for i in txt:
if len(i) > len(longest):
longest = i
print(longest)
+ 2
Try this:
txt = input()
txt.split()
txt[0]=max
for i in txt:
if len(txt[i])>max:
max=len(txt[i])
print(max)
+ 2
Again, here is Simba's elegant solution
print(max(input().split(), key=len)
Here is a fast hybrid that prints all words that are max length.
txt = input().split()
mx=len(max(txt, key=len))
print(list(filter(lambda w: len(w)==mx, txt)))
Use this last line replacement to print only the words on separate lines:
print(*list(filter(lambda w: len(w)==mx, txt)), sep='\n')
+ 2
"Again, here is Simba's elegant solution
print(max(input().split(), key=len)"
^ I started out in that direction but was just using len I think (haven't learned key=len) and it was giving me the word with the highest "letter value" 😕 that's definitely my favorite way though. Brian
+ 2
Wijdene Madiouni I see that you joined only 9 days ago. (Welcome!). DM may get enabled only after 21 days, though you can receive.
+ 2
Brian okay i got u
+ 1
Sorry for annoying u but can u write the whole code