I am fairly new to programming and I was just introduced to the concept of recursion. I've been presented with the famous Towers of Hanoi problem. Here is how the guy in the course I'm following has solved it like this:
def printmove(fr,to):
print('move from'+ str(fr)+'to'+str(to))
def Towers(n,fr,to,spare):
if n == 1:
printmove(fr,to)
else:
Towers(n-1,fr,spare,to)
Towers(1,fr,to,spare)
Towers(n-1,spare,to,fr)
Towers(4,"P1","P2","P3")
What I do not understand is (and most probably it's pretty obvious but I just can't wrap my head around it) how does it know when to pass to the second recursive call Towers(1,fr,to,spare)?