30 lines
895 B
Python
30 lines
895 B
Python
import io
|
|
|
|
def hanoi(n, source, auxiliary, target):
|
|
if n < 1:
|
|
print(f"No disks, nothing to do")
|
|
return
|
|
if n == 1:
|
|
print(f"Move disk 1 from {source} to {target}")
|
|
return
|
|
# Move n-1 disks from source to auxiliary, so they are out of the way
|
|
hanoi(n - 1, source, target, auxiliary)
|
|
# Move the nth disk from source to target
|
|
print(f"Move disk {n} from {source} to {target}")
|
|
# Move the n-1 disks that we left on auxiliary to target
|
|
hanoi(n - 1, auxiliary, source, target)
|
|
|
|
# Example usage:
|
|
# hanoi(3, 'A', 'B', 'C')
|
|
|
|
if __name__== "__main__":
|
|
user_input = input("Please enter an integer: ")
|
|
try:
|
|
user_integer = int(user_input)
|
|
print(f"You entered the integer: {user_integer}")
|
|
except ValueError:
|
|
print("That's not a valid integer. Please try again.")
|
|
|
|
hanoi(user_integer,'A','B','C')
|
|
|