Reverse a string code not working


#1

For some reason my solution to the problem isn’t accepted, but my code works in terminal on my mac. Am i doing something wrong?

def reverse(text):
  new_text = []
  for index, char in enumerate(text):
    new_text.append(text[(len(text) - 1) - index])
    return "".join(new_text)

#2

That line (the return statement) should not be indented so much. That makes it part of the loop block.


#3

Thank you. Im still not used to indenting.


#4

One habit to form that will help prevent the above error is to write the block with the return, first, then fill in the missing bits.

def reverse(test):

    return # expression

Now complete the function and fill in the expression to be returned. Note that above I used four spaces for indent. You can use two if you prefer.