Skip to content

Instantly share code, notes, and snippets.

@R3DHULK
Created March 8, 2023 19:42
Show Gist options
  • Save R3DHULK/f1cefa8a6d0c83ee7eef85f9803313ce to your computer and use it in GitHub Desktop.
Save R3DHULK/f1cefa8a6d0c83ee7eef85f9803313ce to your computer and use it in GitHub Desktop.
Text Based Tower Of Hanoi In Python
def hanoi(n, source, auxiliary, target):
if n > 0:
# Move n-1 discs from source to auxiliary
hanoi(n-1, source, target, auxiliary)
# Move the nth disc from source to target
print(f"Move disk {n} from {source} to {target}")
# Move the n-1 discs from auxiliary to target
hanoi(n-1, auxiliary, source, target)
# Get the number of discs from the user
n = int(input("Enter the number of discs: "))
# Call the hanoi function to solve the puzzle
hanoi(n, 'A', 'B', 'C')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment