From 865daab1c3c2233d49d8c19b0963c19aaf0f8bf8 Mon Sep 17 00:00:00 2001 From: Krrish Verma <99977228+Krrish2512@users.noreply.github.com> Date: Sat, 14 Oct 2023 21:08:15 +0530 Subject: [PATCH] Create Tower_of_Hanoi.py --- Tower_of_Hanoi.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Tower_of_Hanoi.py diff --git a/Tower_of_Hanoi.py b/Tower_of_Hanoi.py new file mode 100644 index 0000000..b03ecf4 --- /dev/null +++ b/Tower_of_Hanoi.py @@ -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()