0

I want to know how does (num-1) work in line number 3. In my head it looks like this: num+(num-1) = 5 + (5-1) = 5 + 4 = 9. But later I realized that wasn't the case. How does it work?

THIS IS MY CODE:

def find_sum(num):
    if num!=0:
        return num + find_sum(num-1)
    else:
        return num
num = int(input("Enter a number: "))
print("The sum of the first " , num ," integers is ", find_sum(num))
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
seyan
  • 1
  • 1
  • Welcome to Stack Overflow. "In my head it looks like this: num+(num-1) " Notice how the line actually says `num + find_sum(num-1)`? Do you see how this is different from the first step in your reckoning? – Karl Knechtel Sep 23 '22 at 06:10

0 Answers0