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.


refers to this,

Did you mean to write, text[n]?


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:


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,

return "".join(r)


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