jeudi 26 juillet 2018

The logic of if statement with recursive function in Python

I have the following simple recursive function:

def draw_interval(center_length):
if center_length > 0: 
    print('first value: {}'.format(center_length))
    draw_interval(center_length - 1)      
    print('second value: {}'.format(center_length))              

draw_interval(3)

and the output is:

first value: 3
first value: 2
first value: 1
second value: 1
second value: 2
second value: 3

My question is why that is the case, and the function runs draw_interval(center_length - 1) even after center_length > 0 is False. I already saw a similar question, but my question points to a different angle of recursive functionalities and conditional statements in Python.

Aucun commentaire:

Enregistrer un commentaire