Reverse is adding "abc" in front of Phython?


#1



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


Your function fails on reverse("Python!"). It returns "!nohtyPcba" when it should return "!nohtyP".


If I call a function of my own, such as reverse("test"), then I get the expect result of "tset". However, when I run it without calling my own function and it is testing Python!, then somehow this "abc" shows up in reverse "cba". I have no idea where that is coming from..??


word=[]
def reverse(text):
    for char in text:
        word.append(char)
    print word
    
    length=(len(word))
    done=""
    while length>0:
        done+=(word[length-1])
        print done
        length-=1
    print done
    return done


#2

For whatever reason, after I removed some of the redundant code in the function, it is now working properly. (shrug)

Feel free to explain why I was seeing the "abc" still though. Otherwise, the below code works for this. Thanks!

def reverse(text):
    length=(len(text))
    word=""
    while length>0:
        word+=(text[length-1])
        print word
        length-=1
    print word
    return word

#3

Defining the working list outside of the function means it is never reset.


#4

oh, okay. I can see how that would cause some problems. Thanks


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.