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.
function return None
by default, so you should return backwards
explicitly
also, you can use a pythonic way to solve the problem:
letters[::-1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With