Reverse help?


#1



Hi everyone. I'm stuck on getting my code for reverse to work correctly. https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096#

I'm getting this mesage:

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


def reverse(text):
    n = []
    n.append(str(text))
    for char in range(len(n), 0):
        return char


#2

if you want to loop in reverse, range will need a third argument: step, by default step is +1


#3

def reverse(text):
    n = []
    n.append(str(text))
    for char in range(len(n), 0, -1):
        return char

Now I'm getting the error message "Your function fails on reverse("Python!"). It returns "1" when it should return "!nohtyP"."


#4

a function ends the moment a return keyword is reached

so you want to reverse the string using the for loop, and then after the loop return the reversed string


#5

Your "char" is a number and function returns this number (first number, returned by range function). You need use 'char' as index to take one letter from "text". Read carefully help(range).
I think there is no need to use lists. String is an iterable object. You can't change it, but you can make new empty string variable and pass into it letters from "text" in order you want, using "text[char]".


#7

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