8

Here is my program

def reverse(letters):
    backwards = ""
    i = len(letters) - 1
    while i >= 0:
        backwards = backwards + letters[i]
        i = i - 1
    print (backwards)

print (reverse("hello"))

It works, it prints out "olleh" but after, it prints "None" on a new line. And I'm asking why this is. Obviously the program is to reverse a word, the code works, and without a function it doesn't print none, so I don't know why it does in the function. This is being used in another larger program, so I need it as a function, and because it's for school, I'm not allowed to simply use the .reverse() function. So I really need this code fixed rather than large changes, if possible.

Alfe
  • 56,346
  • 20
  • 107
  • 159
user2832964
  • 115
  • 1
  • 5

6 Answers6

4

function return None by default, so you should return backwards explicitly

also, you can use a pythonic way to solve the problem:

letters[::-1]
liuzhijun
  • 4,329
  • 3
  • 23
  • 27
3

You can use a return statement to exit the function returning a value. If the function gets to the end without reaching a return statement, it will return None by default

def add1(x):
   return x+1

def returnsNone():
   pass

print(add1(2))
print(returnsNone())
hugomg
  • 68,213
  • 24
  • 160
  • 246
1

Every function returns something in Python. If you don't explicitly return a value, Python has your function return None.

Your function doesn't actually return anything because print prints to stdout, while return actually returns a value. They may look the same in the REPL, but they're completely different.

So to fix your problem, return a value:

return backwards
Blender
  • 289,723
  • 53
  • 439
  • 496
1

Try just:

 reverse(hello) 

In fact with

print reverse(hello) 

you are printing the return value of reverse. And that return value is None.

Let me give you few general advises:

reverse() in your code is a function with side effects (printing). You should avoid functions with side effects when you don't need, consider having reverse() returning the word instead of printing it:

def reverse(letters):
    backwards = ""
    i = len(letters) - 1
    while i >= 0:
        backwards = backwards + letters[i]
        i = i - 1
    return backwards
print (reverse("hello"))

Moreover

i = len(letters) - 1
    while i >= 0:
        backwards = backwards + letters[i]
        i = i - 1

is not easy to mantain and if you add functionality to the loop the decrement i=i-1 will be far from the place where it should 'conceptually' be. You should prefer having decrement together with the check:

for i in xrange(len(letters)-1,-1,-1): 
    backwards = backwards + letters[i] 

When I'm lazy I write

myString = myString + fewChars

so I can understand you being lazy. But adding fewChars does not modify myString but creates a new one. If you are iteratively adding many chars, rarely the most efficient way is to add one by one. Consider using join(). For instance

letters = 'word'
lettersList = [letters[i] for i in xrange(len(letters)-1,-1,-1)]
myReversed ''.join(lettersList)

ok I agree is not readable and probably not even faster, but for larger strings it scales better than a newString = oldString + oneChar approach.

That said, more pythonic approaches

letters[::-1] 

already suggested by someone faster than me typically works much better and are easy to be read by python programmers.

jimifiki
  • 5,377
  • 2
  • 34
  • 60
0

It makes sense if you think about it. Your reverse does not return anything - it just prints it's result. But when you write print (reverse("hello")), you are actually printing what reverse returns. Since it doesn't return anything, None is printed.

Idan Arye
  • 12,402
  • 5
  • 49
  • 68
0

As mentioned earlier. If the python function does not come across a return statement it prints None by default. Making a small change as shown below fixes this:

def reverse(letters):
    backwards = ""
    i = len(letters) - 1
    while i >= 0:
        backwards = backwards + letters[i]
    i = i - 1
    return(backwards)  # instead of print(backwards)

print(reverse("hello"))
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49