Reverse words

Language: Python


To complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained.


def reverse_words(text):

     """This function reverses each other words in a sentence but keeps the words in the same place"""

#Split the sentence into a list so that the words can be worked at separately rather than reversing the whole list
text=text.split(" ")  
    #Go through each of the words reversing them and turning them into lists
for i in range (0,len(text)):
    #Join the lists together to make a list of reversed words
for i in range (0,len(text)):
#Join the whole list together to make a reversed sentence
text= " ".join(text)
return text

text = input ("Please give me a sentence.")
 print (reverse_words(text))