'python recursion function return [duplicate]
I want to make decimal-binary converter using recursion. I've done it, but my function (below) returns None.. If I change return clause to print - it's work properly. But I want returning here! What's the problem?
def my_bin(num, result = ''):
if num == 0:
return result[::-1]
else:
result += str(num%2)
my_bin(num//2, result)
my_bin(5) # None
Solution 1:[1]
One return is missing:
def my_bin(num, result = ''):
if num == 0:
return result[::-1]
else:
result += str(num%2)
return my_bin(num//2, result)
my_bin(5)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Brinfer |
