Reverse


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096#


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


I expect it to spit out my word backwards. I'm not sure what I'm doing wrong?


def reverse(text):
    word = " "
    l = len(text)-1
    if l > 0: 
       for n in range(l):
           word = word + text[l]
           l -= 1
           return word
           print word
    else :
        return false


#2

a function ends the moment a return keyword is reached, in your case in the first run of the loop. You might want to let the whole loop run before returning the reversed string


#3

Now I get the error:
Oops, try again. Your function fails on reverse("Python!"). It returns " !nohtyP" when it should return "!nohtyP".

Thanks so much for you help

def reverse(text):
    word = " "
    l = len(text)-1
    if l > 0: 
       for n in range(l+1):
           word = word + text[l]
           l -= 1
    else :
        return false
    return word

#4

here:

word = " "

word should be set to an empty string, otherwise you have an additional space in the beginning of the reversed sentence


#5

You are a Python Jedi, and the force is with you sir or ma'am. Thanks! Can't believe I got most of it by myself, you guys are great, keep up the great support!


#6