Answer the question
In order to leave comments, you need to log in
Towers of Hanoi in Python?
Please help me, I've been sitting all day and I don't understand how the function works.
Here is the code:
def move(n, start, finish) :
if n==1:
print(n, start, finish)
else:
tmp= 6 - start - finish
move(n - 1, start, tmp)
print(n, start, finish)
move(n - 1, tmp, finish)
n=int(input())
move(n, 1, 3)
n-1
Answer the question
In order to leave comments, you need to log in
def hanoi(a, b, c, n):
if n == 1: # рекурсивное конечное условие
print(a, '->', c)
else:
hanoi(a, c, b, n - 1)
print(a, '->', c)
hanoi(b, a, c, n - 1)
hanoi('A', 'B', 'C', 5)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question