How to program for tower of hanoi in python

How to program for the tower of hanoi in python.

  1. def hanoi(disks, source, auxiliary, target):
     if disks == 1:
    print('Move disk 1 from peg {} to peg {}.'.format(source, target))
     hanoi(disks - 1, source, target, auxiliary)
     print('Move disk {} from peg {} to peg {}.'.format(disks, source, target))
     hanoi(disks - 1, auxiliary, source, target)
    disks = int(input('Enter number of disks: '))

    hanoi(disks, 'A', 'B', 'C')


Post a Comment