I am trying to write python code that takes words from an array and makes a new array that includes all of the longest words of the previous array. I can't find where the problem is, but whenever I run this, it just eats a ton of RAM but doesn't work (it should print the words Good
and Cool
). Does anyone see the problem?
words = ["Good", "Bad", "Cool"]
def longest_word():
longest = [""]
for word in words:
for word2 in longest:
if len(word) > len(word2):
longest.clear()
longest.append(word)
elif len(word) == len(word2):
longest.append(word)
print(str(longest_word))
longest_word()