+ 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

18th Aug 2022, 3:21 PM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar
2 Respuestas
+ 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.
18th Aug 2022, 3:32 PM
Jayakrishna 🇮🇳
+ 4
18th Aug 2022, 3:45 PM
Siavash Kardar Tehran
Siavash Kardar Tehran - avatar