0
Can anyone help me to find out the mistakes?
Given a text as input, find and output the longest word. Sample Input this is an awesome text Sample Output awesome My code: txt = input() word = txt.split() for x in word: num = max(range(len(x))) if "," in x: num = num - 1 out = (num, x) print(out[1])
1 Answer
+ 1
You making it really complex!
Try this way.
i) Split the string
ii)Now compare length of every word in array with previous one. If the length is greater then previous one update the length variable(ln) and also store that word in other variable (ln_word).
Now outside loop check the last updated word.
txt = input()
word = txt.split()
ln = 0
ln_word = ""
for x in word:
if len(x) > ln:
ln = len(x)
ln_word = x
print(ln_word)