Search code examples
pythonmathvscode-debugger

Why is 0 larger than 0 (according to the vscode debugger)?


On line 7 there is a variable x which is equal to 0. When I run the program, it should not advance to the next indent if x is not greater than 0. But for some reason it does.

0 > 0 is True

What is going on?

class MyProblem():
    def __init__(self):
        self.x = 5
    def recursive(self):
        self.x -= 1
        if self.x > 0:
            self.recursive()

p = MyProblem()
p.recursive()

Solution

  • The reason you are seeing the debugger highlight the line self.recursive() when self.x is 0 is because it is popping the call stack: recall, you entered this recursive function when x was 5, 4, 3, 2, 1. Once x reaches 0, you see the debugger leaving the recursive function for 1, 2, 3, 4, and 5, which is why stepping on that line leaves you at the same line.