[Python] Why and How Recursion X variable incease after satified 0 in the IF condition inside...

Discussão em 'Python' iniciado por Stack, Fevereiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I understand the recursion function up until the point where the IF condition with zero is satisfied, which results in a return value of True. In the is_odd() function, the negation reverses this return value.

    I try to understand it using a Python debugger. For a more detailed explanation, I uploaded a clip. Please watch it and focus on the local & watch sections on the left side of the vscode. Where X variable decreases until 0 and then increases until invoking value. Here is a YouTube short Clip link: https://www.youtube.com/watch?v=zUbZDCUkwa0&t=15s

    However, I'm curious about how and why the variable X increases (e.g., from 0 to 1, 2, 3, 4, 5) after the IF condition returns True and more things it enters in ELSE condition until it reaches the invoked value of either 5 or 7.

    I would appreciate it if you could explain this to me.

    Here is the code:

    def is_even(x):
    if x == 0:
    bol = True
    return bol
    else:
    ahOdd = x-1
    rtn = is_odd(ahOdd)
    return rtn

    def is_odd(x):
    ahEven = is_even(x)
    return not ahEven

    invokingOdd = is_odd(5)
    invokingEven = is_even(7)
    print(invokingOdd)
    print(invokingEven)


    [​IMG]

    Continue reading...

Compartilhe esta Página