Home »
Python »
Python Programs
Python program Tower of Hanoi (modified)
Here, we are going to implement a python program for Tower of Hanoi.
By Anuj Singh Last updated : January 04, 2024
Read the detailed solution here: Python program for Tower of Hanoi
So here is the code:
def hanoi(x):
global repN
repN += 1
if x == 1:
return 2
else:
return 3*hanoi(x-1) + 2
x = int(input("ENTER THE NUMBER OF DISKS: "))
global repN
repN =0
print('NUMBER OF STEPS: ', hanoi(x), ' :', repN)
Output:
ENTER THE NUMBER OF DISKS: 14
NUMBER OF STEPS: 4782968 : 14
Python Basic Programs »