Practice Makes Perfect-Reverse


#1



Can someone please explain why I'm getting Oops, try again. Your function fails on reverse("Python!"). It returns "cba!nohtyP" when it should return "!nohtyP".

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


_new=[]
def reverse(text):
    x=len(text)
    for num in range(0,int(len(text))):
        _new.append(text[int(x-1)])
        x-=1
    return "".join(_new)


#2

The problem is the result of not using a locally defined list. Every run of the program continues to add to the list. Define your list inside the function and it should work.

range(x)

will work since x is defined, and it is an integer already (len() returns an int).

text[x - 1]

is sufficient.


#3

The more pythonic way is text[::-1]


#4

I agree; however, that isn't my point specifically, but simplification of the OP code.