Reverse


#1

I am stuck at the reverse string excercise

My code is this

def reverse (x):
    n = len(x) - 1
    s = ' '
    while n >= 0:
        s = s + x[n]
        n = n -1
    return s

It gives an error saying
"Oops, try again. Your function fails on reverse("Python!"). It returns " !nohtyP" when it should return "!nohtyP".

My output is correct but still I couldn't pass the exercise. I don't know why.

`


#2

lets replace the space with a underscore:

"_!nohtyP"

for some reason, your code has a space at the beginning, how can you remove it?


#3

Thank you. I also just deleted the space in this step and it worked fine s = ' '


#4

yea, s should be set to an empty string, otherwise you get the additional space like discovered. Good job fixing it :slight_smile:


#5