0

Longest word - Can i solve them with functions? Can anyone help?

I am stuck in a question and tried many times but can't solve this out. Look what i did, txt = input() def longest() return longest longest(txt.split(' '))

4th Jan 2023, 6:41 PM
Haris
Haris - avatar
5 odpowiedzi
+ 1
Hi Haris, Python has in the max method an optional argument key which can be used to test on the length. It works for Strings on lists, so you need to split your string into a list. e.g.: print(max(txt.split(), key = len)) where txt is the variable with the full string.
4th Jan 2023, 7:02 PM
JuZip
+ 1
Julian Zimpel txt = input() print(max(txt.split(' '), key = len)) but they didn't told about max that contains key method but okay. I Got it, thank you!
4th Jan 2023, 7:20 PM
Haris
Haris - avatar
+ 1
since you already learned about max, and while i am big fan of loops, here a nice code to search for longest word i just wrote( you can consider it a bita version) simple and easy, give it a try :) txt= input() lst_txt=txt.split(" ") max=0 for word in lst_txt: if len(word)>max: max=len(word) longest=word print(longest)
4th Jan 2023, 9:42 PM
Ali M
Ali M - avatar
0
Ali M thank you ali 👍🇵🇸 falastini
4th Jan 2023, 11:42 PM
Haris
Haris - avatar
0
you are so welcomed, my brother 🤗
4th Jan 2023, 11:44 PM
Ali M
Ali M - avatar