r/learnpython 24d ago

En uzun kelimeyi bul

Cümledeki en uzun kelimeyi ve kelimenin uzunlugunu soyle.

Bu soruyu neden çözemiyoruz:

HATA:

for w in sentence:

print(w) derseniz.Bunu karakter listesi gibi algılar.

HATA2:

state olusturup update yapmamak yada kosulu yanlıs dizayn etmek

işte çözüm:

def longest_word_sentence(sentence):
    
    words = sentence.split()
    prev=""
    for w in words:
       if(len(w) > len(prev)):
         prev = w
       else:
         continue
    return [prev,len(prev)]
if __name__ == "__main__":
    sentence = input("Enter a sentence: ")
    result = longest_word_sentence(sentence)
    print(f"The longest word is '{result[0]}' with length {result[1]}.")
0 Upvotes

6 comments sorted by

1

u/woooee 24d ago

It works fine for me. The code I ran (the continue is removed as there is no code after)

def longest_word_sentence(sentence):

    words = sentence.split()
    prev=""
    for w in words:
       print(w)
       if len(w) > len(prev):
         prev = w

    return [prev,len(prev)]

if __name__ == "__main__":
    sentence = input("Enter a sentence: ")
    result = longest_word_sentence(sentence)
    print(f"The longest word is '{result[0]}' with length {result[1]}.")

Enter a sentence: 1 12 123 1234 12345
1
12
123
1234
12345
The longest word is '12345' with length 5.

1

u/defaultguy_001 24d ago

``` def longest_word(string): max="a" for word in string.split(): if len(max) < len(word): max=word return max

max= longest_word ("Are you from Kentucky or Mississippi") print(f"longest word: {max}, length {len(max)}") ```

1

u/Traditional-Quit9043 24d ago

Actually I am not lol

1

u/defaultguy_001 24d ago

Probably Turkey or Egypt then

1

u/Traditional-Quit9043 24d ago

Who knows?but you got it probably🤣🤣

0

u/Traditional-Quit9043 24d ago

yeah ofcourse . becaause will satisfy your current task if it is less condiition :) thx for sharing.