Why does python both return answer and none?

I am writing a function that will return an argument ,which is a text, in reverse ,however it returns both output and none. I can’t figure it out, can someone explain to me why it returns none

def reverse(text):
  x=[text]
  for i in x:
    index=len(i)-1
    while index>=0:         
      print i[index],
      index-=1
print reverse("code") -------> e d o c None
2 Likes

If I’m not mistaken, it could because of the spacing, and then not spacing for none.

I hope this helps =)

2 Likes

returning or printing? Which does what?

You’re printing from two places, so perhaps it shouldn’t be surprising that you see two kinds of outputs. If only one of them should print, then what should the other do, if anything?

Reversing twice should produce the original value. Can you use your function to make that happen? If not, does that say something about how your function should behave differently from what it currently does?

How many elements does [text] have? If you meant to convert to list, what that does is create a list which has one value in it, text, similar to: [1]

2 Likes

Nothing of the sort. It has to do with there being a print statement inside the function and another on the caller. The latter is printing None because print does not have a return.

I’m not convinced this code is really doing what it is meant to do. text is a string, so why this line,

x = [text]

?

And, why the while loop? Should not the for loop be adequate for the task of iterating over a string? Recall that the instructions ask us to RETURN a reversed string. Where is your return statement?

2 Likes