Code 1: import math def average (numbers): return sum(numbers)/len(numbers) def…
- User
Code 1: import math def average (numbers): return sum(numbers)/len(numbers) def isPrime(n): if n<=1: return False for i in range(2,math.isqrt(n)+1): if n%i==0: return False return True def isGreater(a,b): if(a>b): print(f"{a} is greater than {b}") elif(a==b): print(f"{a} is equal to {b}") else: print(f"{b} is greater han {a}") print("\n") print("Welcome to our calculator".center(50,"")) print("\n") while True: try: choice=float(input( "What do you want to calculate?\nHere are the options:\n"
"1: Avarge of multiple numbers.\n"
"2: Check whether a number is prime or not.\n"
"3: Check which number is greater between the 2 numbers.\n" "4: Exit.\n" "Now enter your choice (Only enter corrosponding serial numbers eg: 1, 2, 3, 4): ")) match choice: case 1: while True: try: number=input("Enter the numbers separeted by comma: ").strip().split(",") a=[float(num) for num in number] print("The avarage is: ", average(*a)) except ValueError: print("Please enter multiple numbers separated by comma: ") choice=input("Do you want to continue avarage calculation? (y for yes/n for no/exit): ").strip().lower() if choice=="yes" or choice=="y": continue elif choice=="no" or choice=="n": print("\n") break elif choice=="exit": exit() else: print("Invalid choice. Continuing anyway...") print("\n") case 2: while True: try: number=int(input("Enter a number: ")) if isPrime(int(number))==True: print(f"{int(number)} is a prime number.") else: print(f"{int(number)} is not a prime number.") except ValueError: print("Please enter a valid number: ") choice=input("Do you want to continue prime number calculation? (y for yes/n for no/exit): ").strip().lower() if choice=="yes" or choice=="y": continue elif choice=="no" or choice=="n": print("\n") break elif choice=="exit": exit() else: print("Invalid choice. Continuing anyway...") case 3: while True: try: a=float(input("Enter the first number: ")) b=float(input("Enter the second number: ")) isGreater(a, b) except ValueError: print("Please enter a valid number: ") choice=input("Do you want to continue greater number calculation? (y for yes/n for no/exit): ").strip().lower() if choice=="yes" or choice=="y": continue elif choice=="no" or choice=="n": print("\n") break elif choice=="exit": exit() else: print("Invalid choice. Continuing anyway...") case 4: exit() case _: print("Invalid choice. Please enter a valid option.") except ValueError: print("Please enter a valid choice: ") print("\n") Code 2: import math AVERAGE_CALCULATION = 1 PRIME_CHECK = 2 GREATER_NUMBER = 3 EXIT = 4 def average(numbers): """Calculate the average of multiple numbers.""" return sum(numbers) / len(numbers) def isPrime(n): """Check if a number is prime.""" if n <= 1: return False for i in range(2, math.isqrt(n) + 1): if n % i == 0: return False return True def isGreater(a, b): """Compare two numbers and return a formatted string.""" if a > b: return f"{a} is greater than {b}" elif a == b: return f"{a} is equal to {b}" return f"{b} is greater than {a}" def get_continuation_choice(operation_name): """Handle the continuation choice for calculations.""" choice = input(f"Do you want to continue {operation_name} calculation? (y for yes/n for no/exit): ").strip().lower() if choice == "exit": exit() return choice in ["yes", "y"] def main(): print("\n") print("Welcome to our calculator".center(50, "")) print("\n") while True: try: choice = int(input( "What do you want to calculate?\nHere are the options:\n" "1: Average of multiple numbers.\n" "2: Check whether a number is prime or not.\n" "3: Check which number is greater between the 2 numbers.\n" "4: Exit.\n" "Now enter your choice (Only enter corresponding serial numbers eg: 1, 2, 3, 4): ")) match choice: case 1: while True: try: numbers = input("Enter the numbers separated by comma: ").strip().split(",") a = [float(num) for num in numbers] print("The average is: ", average(*a)) except ValueError: print("Please enter multiple numbers separated by comma: ") continue - Assistant