Q:

How do I make words of certain length flip?

So I'm trying to create a program that takes sentences and makes words of length 5 or longer reverse itself. At the moment it is only flipping the last word that meets the condition and I don't know why.
userInput = "Hello this is a test sentence"
wordList = userInput.split()

for i in wordList:
    if len(i) >= 5:
        reversedWord = i[::-1]
        print(reversedWord)
        reversedSentence = userInput.replace(i, reversedWord)

print(reversedSentence)

Instead of outputing "olleH this is a test ecnetnes" it outputs "Hello this is a test ecnetnes"

A:

You keep replacing the original userInput
reversedSentence = userInput
for i in wordList:
    if len(i) >= 5:
        reversedWord = i[::-1]
        print(reversedWord)
        reversedSentence = reversedSentence.replace(i, reversedWord)

You need to keep updating the reversedSentence variable instead.

