+ 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

15th Aug 2019, 3:06 PM
Evan Martine
6 Respostas
+ 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.
15th Aug 2019, 3:18 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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!
15th Aug 2019, 4:32 PM
Evan Martine
+ 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
15th Aug 2019, 3:39 PM
Evan Martine
+ 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
15th Aug 2019, 3:55 PM
Hatsy Rei
Hatsy Rei - avatar
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" ...
15th Aug 2019, 3:41 PM
Evan Martine
0
I'm still semi-new to recursion. The whole mindset is different from OOP, IMO
15th Aug 2019, 3:59 PM
Evan Martine