How to Find If Two Numbers Are Amicable in Python
In this tutorial, we will learn how to program "How to Find If Two Numbers Are Amicable in Python." The objective is to determine whether the two given numbers are amicable or not. This tutorial will guide you step by step through the entire process of checking and calculating amicable numbers. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively, helping you strengthen your problem-solving abilities and enhance your Python coding skills.
This topic is straightforward to understand. Simply follow the instructions provided, and you will complete it with ease. The program will guide you step by step through the process of finding the amicable between two number. So, let’s dive into the coding process!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- def sum_of_divisors(n):
- """Return the sum of proper divisors of n (excluding n itself)."""
- total = 1 if n > 1 else 0 # 1 is always a divisor (except for n=1)
- for i in range(2, int(n**0.5) + 1):
- if n % i == 0:
- total += i
- if i != n // i: # avoid adding square root twice
- total += n // i
- return total
- while True:
- print("\n=============== Find If Two Numbers Are Amicable ===============\n")
- try:
- x = int(input('Enter number 1: '))
- y = int(input('Enter number 2: '))
- if x <= 0 or y <= 0:
- print("Please enter positive integers only.")
- continue
- if sum_of_divisors(x) == y and sum_of_divisors(y) == x:
- print(f"{x} and {y} are Amicable! 🎉")
- else:
- print(f"{x} and {y} are Not Amicable.")
- except ValueError:
- print("Invalid input. Please enter integers only.")
- continue
- opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
- if opt == 'no':
- print("Exiting program...")
- break
- elif opt != 'yes':
- print("Invalid choice. Exiting program...")
- break
Output:

There you have it we successfully created How to Find If Two Numbers Are Amicable in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language