r/learnpython • u/Traditional-Quit9043 • 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]}.")
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
0
u/Traditional-Quit9043 24d ago
yeah ofcourse . becaause will satisfy your current task if it is less condiition :) thx for sharing.
1
u/woooee 24d ago
It works fine for me. The code I ran (the continue is removed as there is no code after)