+ 4
Please i need easy and appropriate explanation to this code.
def recur(0): if n==0 return 0 else: return n + recur (n-1) print(recur(3)) how comes answer is 6?? đ
2 Answers
+ 4
oh, I see... thanks đ
+ 2
when you print recur (3), what goes through is: n is not equal to 0, thus n = 3 + recur (2) + recur (1) which equals 6.
That's because recur calls itself, adding the sum of its number minus 1 (3 + 2 + 1) and exits only when recur gets to 0