+ 7

Can you please explain the output of this code?

def rec(n): n %= 5 if n <= 1: return n else: return rec(n-1) + rec(n-2) print(rec(9))

3rd Mar 2020, 4:01 PM
APC (Inactive for a while)
APC (Inactive for a while) - avatar
3 Answers
+ 4
I will try to explain and I made the code different so you can check the results: def rec(n): n %= 5 if n <= 1: return n else: return rec(n-1) + rec(n-2) print(rec(0)) # upper return so it will be 0 print(rec(1)) # upper return so it will be 1 print(rec(2)) # lower return so it will be rec(1) + rec(0) == 1 print(rec(3)) # lower return so it will be rec(2) + rec(1) == 2 print(rec(4)) # lower return so it will be rec(3) + rec(2) == 3 # 9 % 5 == 4 so rec(9) == rec(4)
3rd Mar 2020, 6:00 PM
Paul
Paul - avatar
0
It's called recursive function. When function call itself recursion happen. Check out this video for better understanding:https://youtu.be/XkL3SUioNvo
3rd Mar 2020, 8:06 PM
Jesse Pinkman
Jesse Pinkman - avatar
0
_ i in range(0, 20,_ ): print(-) fill this blanks
4th Mar 2020, 6:29 AM
Bangash ivy
Bangash ivy - avatar