-
-
Save SRIKALEESWARAR-S/9ba8ebd71fbec2f8a296a8b016476b54 to your computer and use it in GitHub Desktop.
Simple calculator using python for kaniyam.com article
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from math import sqrt | |
| print("""Welcome to this wonderful and very easy-to-learn calculator | |
| created by Srikaleeswarar!""") | |
| print("""In this calculator, you can easily do the following operations: | |
| Addition (1) | |
| Subtraction (2) | |
| Multiplication (3) | |
| Division (4) | |
| Power (5) | |
| Square Root (6) | |
| Please select the operation by entering the corresponding number.""") | |
| try: | |
| operator = int(input("Select the operation that you want to proceed: ")) | |
| if operator > 6 or operator <= 0: | |
| print("Enter a correct option between 1 and 6.") | |
| exit() | |
| a = float(input("Enter the first value: ")) | |
| if operator == 5: | |
| p = int(input("Enter the power value: ")) | |
| print(f"The value of {a}^{p} is:", a ** p) | |
| elif operator == 6: | |
| if a < 0: | |
| print("Error: Cannot calculate square root of a negative number.") | |
| else: | |
| print(f"The square root of {a} is:", sqrt(a)) | |
| else: | |
| b = float(input("Enter the second value: ")) | |
| if operator == 1: | |
| print(f"Sum of {a} + {b} is:", a + b) | |
| elif operator == 2: | |
| print(f"The subtracted value of {a} - {b} is:", a - b) | |
| elif operator == 3: | |
| print(f"The multiplied value of {a} × {b} is:", a * b) | |
| elif operator == 4: | |
| if b == 0: | |
| print("Error: Division by zero is not allowed.") | |
| else: | |
| print(f"The divided value of {a} by {b} is:", a / b) | |
| print("Thank you for using the calculator!") | |
| except ValueError: | |
| print("Invalid input: Please enter numbers only.") | |
| except Exception as e: | |
| print("Sorry folks, there's an error:", e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment