Return Value of None?
Question:
I’m trying to solve a problem on CodeWars with the following specifications:
“Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.”
My solution is as follows:
def persistence(n,count = 0):
n = list(str(n))
if len(n) <= 1 :
count = 0
return count
else :
num_per = 1
for i in n :
num_per = num_per * int(i)
num_dump = num_per
if len(str(num_dump)) > 1:
count += 1
n = num_per
persistence(n,count)
else:
count = count + 1
return count
When I choose any number with more than a single digit, I get a return value of ‘None’. I’ve narrowed down the problem to the last return statement in the second else clause. The number of counts are calculated correctly,but the return statement still returns a value of ‘None’. Any help would be greatly appreciated.
Answers:
Just add return before your persistence line:
return persistence(n, count)
Going off of what @jasonharper suggested, your function has an if/else statement which is decided by len(n) <= 1. If the length of n is less than 1, count (which is 0) is returned.
In your else statement however, you have another if/else statement. In your if statement, which is decided by len(str(num_dump)) > 1, you decide to recursively call the same function, however, you return no value. Therefore, your program most likely makes a lot of recursive calls but has no way of storing the values to be returned at the end of the program.
This can be fixed by replacing
persistence(n, count)
with
return persistence(n, count)
I’m trying to solve a problem on CodeWars with the following specifications:
“Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.”
My solution is as follows:
def persistence(n,count = 0):
n = list(str(n))
if len(n) <= 1 :
count = 0
return count
else :
num_per = 1
for i in n :
num_per = num_per * int(i)
num_dump = num_per
if len(str(num_dump)) > 1:
count += 1
n = num_per
persistence(n,count)
else:
count = count + 1
return count
When I choose any number with more than a single digit, I get a return value of ‘None’. I’ve narrowed down the problem to the last return statement in the second else clause. The number of counts are calculated correctly,but the return statement still returns a value of ‘None’. Any help would be greatly appreciated.
Just add return before your persistence line:
return persistence(n, count)
Going off of what @jasonharper suggested, your function has an if/else statement which is decided by len(n) <= 1. If the length of n is less than 1, count (which is 0) is returned.
In your else statement however, you have another if/else statement. In your if statement, which is decided by len(str(num_dump)) > 1, you decide to recursively call the same function, however, you return no value. Therefore, your program most likely makes a lot of recursive calls but has no way of storing the values to be returned at the end of the program.
This can be fixed by replacing
persistence(n, count)
with
return persistence(n, count)