Suggested code doesn't generate expected result in iPython



<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>
Actually this is the correct code suggested by codeacademy. When i ran it in my computer in iPython and when i provide a string it exactly returns the same string without reversing! that is strange! Do you know why it might be the case ? Any indentation effect?

def reverse(text):
    word = ""
    l = len(text) - 1
    while l >= 0:
        word = word + text[l]
        l -= 1
    return word
<do not remove the three backticks above>


i put the code here:

its perfectly reversing, you didn’t provide the condition to replicate the problem, so difficult to say why you have a problem


Thanks for your interactive reply. I dont know why, but when i typed it again fresh, it works. Do you think that the indentation of “return word” (line 7) might be the reason of it returning the reverse(“python!”) = “python!” and not “!nohtyp” ?

Thanks again :slight_smile:


How should it possible be indented to cause that issue? Placing return outside the function, cause a syntax error. Placing return inside the loop will cause the loop to only make a single iteration (return a single letter), given the return keyword will cause the function to end


I see! Thanks for the clarification. I had encountered single iteration problem also - this clarifies it, too!


by default, a function returns None at the end:

def example():

print example() # prints None

if we want to return something else at the end of the function, we can use the return keyword. The catch is, that this will end the function.


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