Created
March 8, 2023 19:42
-
-
Save R3DHULK/f1cefa8a6d0c83ee7eef85f9803313ce to your computer and use it in GitHub Desktop.
Text Based Tower Of Hanoi In Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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