Reverse help


#1



My code works, prints the string in reverse to the console, but I get an error

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

any thoughts?


my_string="Python!"

def reverse(text):
    x=len(text)
    for i in text:
        print text[x-1],
        x-=1
        if x==0:
            break
reverse(my_string)


#2

you need to return the reversed string

I am afraid you will need to improve upon this code


#3

my_string="Python!"

def reverse(text):
    x=len(text)
    for i in text:
        print text[x-1],
        x-=1
        if x==0:
            break
    return reverse
reverse(my_string)

Nw i get a new error message...

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

the frustrating part is that the code is working


#4

now you just return the function object. You need to modify your code in such a way, that in the end we have a reversed string we can actually return

Currently, you string is not reversed, you simply print it in reverse


#5

OK tries to create a new list (reverse_string) to which I would write the string in reverse, and return it,

my_string="Python!"

def reverse(text):
    x=len(text)
    for i in text:
        reverse_string = []
        print text[x-1],
        reverse_string +=text[x-1]
        x-=1
    return reverse_string
reverse(my_string)

now I get the error message:
Oops, try again. Your function fails on reverse("Python!"). It returns "['P']" when it should return "!nohtyP".

I feel like I'm close, but no cigar


#6

Would including only the first letter suggest that you don't finish the job and instead exit early?

Also note that printing is rather useless to the rest of the code which may have need for reversing something as part of what they themselves are doing.


#8

You took reverse_string as a list. Take it as an empty string:
reverse_string=""


#9

it can be a list, as long as you join the list into a string at the end


#10

Thank you. Got it.Your help is appreciated,


#11