To solve Tower of Hanoi > Python Program

To solve Tower of Hanoi > Python Program

Artificial Intelligence

Program:
#function of tower of hanoi


def tower(n,src,aux,dest):
    if(n==1):
        print('move', n, 'from', src, 'to', dest)
        return
    tower(n-1,src,aux,dest)
    print('move', n, 'from', src, 'to', dest)
    tower(n-1,src,aux,dest)

#function call
n=int(input("Enter the no of Disks: "))
tower(n,'A','B','C')

#>>> 
# RESTART: C:/Users/student/AppData/Local/Programs/Python/Python35-32/toh.py
#Enter the no of Disks: 3
#move 1 from A to C
#move 2 from A to C
#move 1 from A to C
#move 3 from A to C
#move 1 from A to C
#move 2 from A to C
#move 1 from A to C
#>>> 

Comments

Popular posts from this blog

Intermediate Code Generation > C Program