(Solved) 7. reverse | It said that I did not define a function called reverse


Hi. This is probably a really dumb question, but I just don't get why it is throwing me the error. I am on https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096#.
I apologise if the answer is highly obvious. I just don't get it.

It gives me the error: 'Oops, try again. Did you create a function called reverse? Your code threw a "global name 'reverse' is not defined" error.'

I expect it to, well, change the text string in a way that it makes it in reverse. I do not understand why it thinks I never defined the function in the first place. (I think I did?)

letters = []
finish = []

def reverse(text):
    letters = list(text)
    y = len(list)
    if y < 0:
        text = ' '.join(finish)
        #unsure if this is correct
        return text
        x = letters[y]
        return y -= 1


You are overthinking this... Try using this is a template:

    VARIABLE_1 = 0
    VARIABLE_2 = ""
        VARIABLE_1 += 1
    return VARIABLE_2

Once you get it to work, don't just move on. I want to make sure you understand why and how it works. If you have any questions about that, please ask them.


thanks! ill take a look at it and ill get back to ya :slight_smile:


ok, i sort of know how it works, but i have a question.

def reverse(x):
    y = 0
    text = ""
    for char in x:
    #my guess: char is 1 character in x    
        y += 1
        text += x[len(x)-y]
    return text
if x = hello
result should be olleh
line 7 - text += hello[len(hello)-1]
= text += hello[4]
which would give text 'o'?
(and then move on to the rest of the characters)
but how does 'hello[4]' select 'o' from the word hello?
i might have learnt dat already, so if i did i think i kinda forgot or something.
unless hello is a list already or smthing? 


It is working by addressing the index location, which starts at 0:

H e l l o
0 1 2 3 4


Thanks! So all strings have index locations for each character?


In python, strings are iterable objects. Meaning that each letter has its own callable id. For instance:

text = "hello"

print text[0] #This would print "h" to the console.
print text[1] #This would print "e" to the console.
print text[2] #This would print "l" to the console.
print text[3] #This would print "l" to the console.
print text[4] #This would print "o" to the console.


Oh, I see! Thanks so much!


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