0

Couldn't understand this challenge

hello def recur(n): if n == 0: return 0 else: return n + recur(n -1) print(recur(3)) the output is 6 how they got 6!? thank you

21st Aug 2017, 9:08 PM
Mohammed Chami
Mohammed Chami - avatar
4 odpowiedzi
+ 14
it's a recursive function eventually it lined up to: recur(3) 3 + recur(2) 2 + recur(1) 1 + recur(0) this last recur call returns 0 so sum up all the n's as: 3 + 2 + 1 and there you have it ;) 6 it is
21st Aug 2017, 9:14 PM
Burey
Burey - avatar
+ 11
recursion takes a bit to get used to we've all been there ~_~
21st Aug 2017, 9:28 PM
Burey
Burey - avatar
+ 2
thank you so much it's new for me.
21st Aug 2017, 9:20 PM
Mohammed Chami
Mohammed Chami - avatar
+ 1
are you adding the numbers on the left .. or the parenthesis? I'm guessing the former, but better to ask than assume. :) thanks in advance
23rd Aug 2017, 1:22 AM
Jennifer Alexander
Jennifer Alexander - avatar