My function decode() needs to read the text from a .txt file, arrange the number-word pairs in numerical order in the shape of a triangle, and return a string of the last words on each row.
I have successfully arranged the pairs, but when I try to isolate the last words on each row it prints all of the words into the string.
txt_file = ['3 love', '6 computers', '2 dogs', '4 cats', '1 I', '5 you']def decode(): sorted_file = sorted(txt_file) row = 0 last_words_string = '' for i in range(len(sorted_file)): for j in range (0, i + 1): if row >= len(sorted_file): break pairs = sorted_file[row].split() last_word = pairs[-1] last_words_string += last_word +'' row += 1 print(pairs, end='') print() print(last_words_string)decode()Here is my current output:
['1', 'I']['2', 'dogs'] ['3', 'love']['4', 'cats'] ['5', 'you'] ['6', 'computers']I dogs love cats you computersBut I need it to say "I love computers".Any ideas what I am missing?