Reverse(text)


#1

I've seen more elegant solutions to the reverse problem, but I just wanted to know why this doesn't work.

When I run it, I get the "returns '!' when it should return '!nohtyP'" message.

Overlooking the fact that the code won't display the text as it should, why doesn't it loop the same number of times as there are characters in "text"?

def reverse(text):
    y = 1
    for c in text:
        return text[len(text) - y]
        y + 1


#2

I've had this problem before. I believe it's because you have "return" in your for loop. "Return" executes its code then exits the loop, so you only go through the loop once, hence you only getting one character printed out.


#3

Thanks! That makes sense. Going back now to try and make it work.


#4

Just got it working. Thanks again.