Returning the longest word in Python
Dear all! My code should return the longest word in a sentence provided by the user and i should not use "max" or "split" in this exercise. Where's the mistake in my code? txt = input() count1 = 0 count2 = 0 for word in list(txt): for char in word: count1 =+1 if count2 <= count1: count2 = count1 longest_word = word print(longest_word) EDIT: SOLUTION WITHOUT MAX AND SPLIT: txt = input()+' ' count = 0 max_length = 0 word = '' for char in txt: if char != ' ': count += 1 word += char else: if max_length <= count: max_length = count longest_word = word word = '' count = 0 print(longest_word) SOLUTION WITH MAX AND SPLIT: txt = input() longest_word = max(txt.split(), key=len) print(longest_word)