Suggested code doesn't generate expected result in iPython


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/reverse?action=lesson_resume&link_content_target=interstitial_lesson
<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?

```python
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>

#2

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


#3

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:


#4

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


#5

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


#6

by default, a function returns None at the end:

def example():
  pass

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.


#7

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