Reverse


#1



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


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


Console is displaying the reverse result "!nohtyP",but still the program is showing error.please help.


name=[]
def reverse(text):
    for n in range(len(text)):
        p=len(text)-1-n
        name.append(text[p])
    return " ".join(name)

        
print reverse("Python!")


#2

what happens if we want to reverse another string:

name=[]
def reverse(text):
    for n in range(len(text)):
        p=len(text)-1-n
        name.append(text[p])
    return " ".join(name)

        
print reverse("Python!")
print reverse("abc")

ouch, because name is not inside the function, the list will accumulate rather then being an empty list for every string we want to reverse


#3

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