sorry am new to python and i need some help i am trying to understand how python runs recursively, the number of times it executes through the loop of any function. so i wrote this program below but i cant explain the result. can someone help me? d = 0 def test(n): print d, n assert n > 0 if n == 1: print 'i am here' else: global d d = d+1 test(n-1) print d, n test(4) the program executes through the loop eact time decreasing n, until n ==1 but what i wasnt expecting is why the program starts all over again, increasing n. hlp
There are three things that you have to handle in a recursive function: 1. How to recurse again. 2. When to stop recursing. This is called the base case. 3. How to return. This is called unwinding. Map these three things into your code as comments, and look at it.
alright, i will try those and see how it goes. thanks for the reply
Join our real-time social learning platform and learn together with your friends!