The program from the entered string should write out all the words in the column, and then write out the longest word from this string, the code for getting a column of words is already ready, but I don’t know how to manually make the code to search for the biggest word. Help me please. Here is the code itself:

str1 = str(input()) word = "" words = [] k = 0 for i in str1: if i != " ": word += i else: words.append(word) word = "" for j in words: print(j) for j in words: if j > words(k): j += 1 else: k += 1 
  • There is an error - if j> words (k):, but it should be: if j> words [k]: - Michael

2 answers 2

In accordance with the comment to the answer Andrio Skur:

 words = ['a', 'asa1', 'dd', '111'] max_len_word = words[0] for i in range(1, len(words)): word = words[i] if len(word) > len(max_len_word): max_len_word = word print(max_len_word) # asa1 

    Like this:

     def str_split(s): words = [] buf = '' for char in s: if char == ' ': if buf: words.append(buf) buf = '' else: buf += char if buf: words.append(buf) return words def words_print(words): for word in words: print(word) def len_word(word): i = 0 for _ in word: i += 1 return i def find_max(words): temp = 0, '' for word in words: word_length = len_word(word) if word_length > temp[0]: temp = word_length, word return temp[1] words = str_split(input()) words_print(words) print('Max:', find_max(words)) 
    • I know that this code is suitable, but I need a program code that does not include such functions as split or max, and so on. - Michael
    • 6
      @ Mikhail, why not indicate this in the question? - br3t
    • five
      I hope you don’t have to write your own line and list implementations? - Andrio Skur