+ 4
[Solved] Returning from recursive functions.
def function(x): if x == 1: return x else: x -= 1 function(x) # if 'x != 1' returns "None" x = 10 print(function(x)) >>> None --------------------------------------------- def function(x): if x == 1: return x else: x -= 1 return function(x) # if 'x!=1' returns function(x) recursively to reach "function(1) = 1" x = 10 print(function(x)) >>> 1 https://code.sololearn.com/cGEdCfDLyeSR/?ref=app
2 Réponses
+ 6
It's a recursive function so
Main call : function(2) returns None.
and function(1) returns 1
Instead of
function(x) write return function(x) in function.
+ 4
Thank you. Jayakrishna🇮🇳