Reverse


#1


Hi everyone,

I am stuck with reverse is giving me this error:
Oops, try again. Did you create a function called reverse? Your code threw a "global name 'tex_reverse' is not defined" error.


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

you can find below my code,

thank you for your help

Salvatore


def reverse(text):
    text_reverse = ""
    for i in range(len(tex_reverse), -1):
        return text_reverse - i -1
    print tex_reverse


#2

The error is just pointing out that you have a spelling error. tex_reverse should be text_reverse.


#4

thank you I corrected, now is giving me this error:

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


#5

the function needs to return the reversed string, your function only prints the reversed string


#6

ok, so it practically not storing the cycle?

I tried like this, but still it doesn't work:

def reverse(text):
    text_reverse = ""
    for i in range(len(text_reverse), -1):
        return text_reverse + text_reverse - i -1
    print text_reverse

reverse("Hello!")

#7

i am not paying attention

a function ends the moment a return keyword is reached

so your function ends the first iteration/run of your loop

You want the whole loop to reverse the loop and then after the loop return the reversed string


#8

thanks, so if I put return at the moment is not doing the loop, pratically?

meaning that the return should be at the end

I tried like this:

def reverse(text):
    text_reverse = ""
    for i in range(len(text_reverse), -1):
        text_reverse = text_reverse + text_reverse - i -1
        return text_reverse
    print text_reverse

reverse("Hello!")

it is still giving me the same error

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


#9

Wow, the longer i look at your code the more problems i see

return is still inside your loop (which is not running, given the function returns None (the default)

Please explain your argument choice for range(), why len(text_reverse)? text_reverse is an empty string


#12

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