Help with user define functions. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help with user define functions.

def num(n): if n<=0: return else: print (n) num(n-1) print('******') print(n) num(4) The first print(n) statement inside else will print values of n but why do i get values for second print statement inside else, even when n==0 then else is not going to run. But still i get the values of n. Why?

4th Aug 2022, 5:19 AM
Abdur Raheem
2 Answers
+ 5
Abdur Raheem , your code has a recursively call of the function num(). i am not sure if this is what your code should do or if it is by accident. to understandthe the low of this code, you should run your code in a debugger step by step. one of them is "pythontutor". copy your code and paste it in the code editor of this website. then click "visualize execution" to step through the code click "next". https://pythontutor.com/visualize.html#mode=edit
4th Aug 2022, 10:35 AM
Lothar
Lothar - avatar
0
Thanks
4th Aug 2022, 11:22 AM
Abdur Raheem