这是一段递归解决河内塔问题的代码:
def printMove(fr, to):
print('Move from tower ' + str(fr) + ' to tower ' + 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)
n=int(raw_input('Enter the initial number of disks: '))
fr=str(raw_input('Enter the number for the initial tower: '))
to=str(raw_input('Enter the number for the second tower: '))
spare=str(raw_input('Enter the number for the third tower: '))
print(Towers(n,fr,to,spare))
它工作正常,但在打印所有动作后,它也打印“无”,我不知道为什么。
提前致谢