7. Reverse - not sure what is wrong with my code


#1



Oops, try again. Your function fails on reverse("Python!"). It returns "[6, 5, 4, 3, 2, 1, 0, -1]" when it should return "!nohtyP".


Hi! I don't understand why the 'backwards' list contains numbers instead of the corresponding letters in those ranges? How do I make the append function return the value of the string within that range? Now it's returning the range.


def reverse(text):
    backwards = []
    x = len(text)
    while x >= 0:
        for c in range(x-1,x):
            backwards.append(c)
        x = x-1
    return backwards


#2

because range() gives you indexes, you can use this indexes to access the corresponding letters of the string:

x = "python!"
print x[0] // will print p

#3

even if you use the backward list, you will still get some error.


#4

please read the guidelines, posting answers like you just did is not according to the guidelines

i know, but i first helped him with his current issue, explaining where he went wrong (and why), in the hope he can then continue himself, if not, i will help more

Either way, i personally believe this will teach him far more then copying some code and looking at it like: that makes sense.

That is like looking at math exercise (without making them) and then think you can pass the math exam


#5

Oops!! sorry. i never knew


#6

that is why i am telling you :wink: don't worry, it is okay :slight_smile:

i i would recommend you to read this topic:

it explains how to use format so indention will show on the forum


#7

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