Please i need easy and appropriate explanation to this code. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?? 😟

4th Oct 2017, 11:17 AM
Dzoyem Barry
Dzoyem Barry - avatar
2 Answers
+ 4
oh, I see... thanks 👍
4th Oct 2017, 11:32 AM
Dzoyem Barry
Dzoyem Barry - avatar
+ 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
4th Oct 2017, 11:29 AM
Frédéric Charette
Frédéric Charette - avatar