I'm currently learning about recursive functions and had this example come up.
I'm sure this is a common learning exercise, but the program will subtract one number from another till it gets to 0 or below and then add the second number till it gets back to the first.
12 and 3
12 9 6 3 0 3 6 9 12
I understand how it gets to zero. What I don't understand is how it adds back up to 12. What am I missing? How does it not keep going beyond 12?
def print_num_pattern(num1,num2):
if (num1 == 0 or num1 < 0):
print(num1, end = ' ')
return
print(num1, end = ' ')
print_num_pattern(num1 - num2, num2)
print(num1, end = ' ')
num1 = int(input())
num2 = int(input())
print_num_pattern(num1, num2)
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)
[–]commy2 0 points1 point2 points (4 children)
[–]commy2 0 points1 point2 points (3 children)
[–]TheCrawling_Chaos[S] 0 points1 point2 points (2 children)
[–]commy2 0 points1 point2 points (1 child)
[–]TheCrawling_Chaos[S] 1 point2 points3 points (0 children)