I stuck on 7. reverse


#1

i don't know why it doesn't work:

def reverse(text):
for i in range(len(text)-1 ,-1 ,-1 ):
return text[i]

I get the erroe code :
Oops, try again. Your function fails on reverse("Python!"). It returns "!" when it should return "!nohtyP".

Can you help me please ?

Thank you very much for helping


#2

All you have really done is set the loop in this code, you have not done anything with the text.

You can simply say for i in text: which will iterate through the text, your next line should be what is done with that text. After that you return the result.


#3

Thank you but the synthax of my code is :
def reverse(text):
"tab" for i in text:
"tab" "tab"for i in range(len(text)-1 ,-1 ,-1 ):
"tab" tab" "tab" return text[i]

so it should be in the loop, but
I get the error code :
Oops, try again. Your function fails on reverse("Python!"). It returns "!" when it should return "!nohtyP".


#4

for i in range(len(text)-1 ,-1 ,-1 ):

All that is doing is establishing the for loop to be for i in a range. it doesn't actually do anything to the text itself.

Your next line should do something with the text, not simply return it.

The line after that should return it. For one example:


Here we define the method itself:

def reverse(text):

Here we set an empty string for which to enter our letters:

    variable1 = empty string

Here we start our loop:

    for letter in text:

Here we actually do something with the letters:

        variable1 = letter + variable1

Finally, we return our reversed string:

    return variable1

#5

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