Reverse


#1

def reverse(text):
n = len(text) -1
while n>=0:
print text[s],
n -= 1

Hello! It seems that my code is wrong. This is the error message: Oops, try again. Did you create a function called reverse? Your code threw a "global name 's' is not defined" error.

Thanks for helping!

Replace this line with your code.


#2

refers to this,

Did you mean to write, text[n]?


#3

Oh, yes, it's [n]..but it's still not correct. It says "Your function fails on reverse("Python!"). It returns "('!',)" when it should return "!nohtyP".
I can't understand why it's wrong! :frowning:


#4

That's the optimum word, there. return This implies you need to return a string object, the reverse of the one passed into the function. So you will need to build it.

The straight forward way would be to define an empty string, say s, then concatenate it rather than print,

    s += text[n]
return s

If you are of the mind to treat strings as immutable objects then define an empty list, say r, and append the characters to it,

    r.append(text[n])
return "".join(r)

#5

Thank you for the clear explanation! Now it is working!