I have been trying to solve a particular question in my own amateur way from the Python 3 Bootcamp on Udemy. So far, there is an error, I guess, in the append() line of my python code. Here I have provided you with the question and the answer to be expected. Please help me find out the error in my code.
MASTER YODA: Given a sentence, return a sentence with the words reversed master_yoda('I am home') --> 'home am I' master_yoda('We are ready') --> 'ready are We'
def master_yoda(text):
mylist=[text.split()]
print(mylist)
newlist=[]
index=-1
for x in mylist:
newlist.append(mylist[index])
index=index+1
for y in newlist:
print(" ".join(y))
You are using the index wrong. You start at -1, but then skip to 0, so you will choose the last element first, but then the first. Instead, use the following code: