Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Tower_of_Hanoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def tower_of_hanoi(n, source, auxiliary, target):
if n == 1:
print(f"Move disk 1 from {source} to {target}")
return
tower_of_hanoi(n - 1, source, target, auxiliary)
print(f"Move disk {n} from {source} to {target}")
tower_of_hanoi(n - 1, auxiliary, source, target)

def main():
try:
num_discs = int(input("Enter the number of discs: "))
if num_discs < 1:
print("Number of discs must be a positive integer.")
else:
tower_of_hanoi(num_discs, 'A', 'B', 'C')
except ValueError:
print("Invalid input. Please enter a positive integer.")

if __name__ == "__main__":
main()