+ 3
I have a return of "None" in this recursion
I have no idea how this is even possible. Any thoughts? https://code.sololearn.com/cT5tXgnINZFG/?ref=app
6 Answers
+ 3
You are supposed to
return digital_root(newN)
on line 9, or else digital_root never returns if n is not smaller than 10. I'm not quite sure what the code is supposed to achieve though, so I wasn't able to check if the results are correct.
+ 3
Hatsy Rei Since you answered in such a timely manner here is another solution to the problem I found brilliant:
def digital_root(n):
return n%9 or n and 9
It absolutely blew my mind!
+ 2
Hatsy Rei Sorry about that, The code is supposed to take an int value like 943 and do 9 + 4 + 3 then do that over and over until the number is less than 10 and return it. If you print(n) under that if statement before it returns you can get the answer. But I need it to return n not print n
+ 1
If that's the case, then my first post resolves the issue. Your original function returns nothing if n < 10 is false. You should be returning digital_root(newN) on line 11 as in the code below.
https://code.sololearn.com/c1hFh7igM0yv/?ref=app
0
My confusion comes from n being printed if print(n) is placed under the if statement but return wont do what its supposed to in this scenario
...
if n < 10:
print(n) # results 1-9
return n # results in "None"
...
0
I'm still semi-new to recursion. The whole mindset is different from OOP, IMO