Sure! Here's a fully working scientific calculator written in Python. This calculator supports basic arithmetic operations, as well as scientific functions like sine, cosine, tangent, logarithm, exponentiation, square root, and more. It uses the math module for scientific functions and runs in the terminal/command line.
import math
def menu():
print("\nScientific Calculator")
print("Select operation:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Power (x^y)")
print("6. Square root (√x)")
print("7. Logarithm (log base 10)")
print("8. Natural Logarithm (ln)")
print("9. Sine (sin)")
print("10. Cosine (cos)")
print("11. Tangent (tan)")
print("12. Factorial (x!)")
print("13. Degrees to Radians")
print("14. Radians to Degrees")
print("15. Exit")
def get_number(prompt="Enter number: "):
while True:
try:
return float(input(prompt))
except ValueError:
print("Invalid input, please enter a number.")
def main():
while True:
menu()
choice = input("Enter choice (1-15): ")
if choice == '1':
x = get_number("Enter first number: ")
y = get_number("Enter second number: ")
print(f"Result: {x} + {y} = {x + y}")
elif choice == '2':
x = get_number("Enter first number: ")
y = get_number("Enter second number: ")
print(f"Result: {x} - {y} = {x - y}")
elif choice == '3':
x = get_number("Enter first number: ")
y = get_number("Enter second number: ")
print(f"Result: {x} * {y} = {x * y}")
elif choice == '4':
x = get_number("Enter numerator: ")
y = get_number("Enter denominator: ")
if y == 0:
print("Error: Division by zero is undefined.")
else:
print(f"Result: {x} / {y} = {x / y}")
elif choice == '5':
x = get_number("Enter base (x): ")
y = get_number("Enter exponent (y): ")
print(f"Result: {x} ^ {y} = {math.pow(x, y)}")
elif choice == '6':
x = get_number("Enter number to find square root of: ")
if x < 0:
print("Error: Cannot compute square root of negative number in real numbers.")
else:
print(f"Result: √{x} = {math.sqrt(x)}")
elif choice == '7':
x = get_number("Enter number to find log10 of: ")
if x <= 0:
print("Error: Logarithm undefined for zero or negative numbers.")
else:
print(f"Result: log10({x}) = {math.log10(x)}")
elif choice == '8':
x = get_number("Enter number to find natural log (ln) of: ")
if x <= 0:
print("Error: Natural logarithm undefined for zero or negative numbers.")
else:
print(f"Result: ln({x}) = {math.log(x)}")
elif choice == '9':
deg = get_number("Enter angle in degrees: ")
rad = math.radians(deg)
print(f"Result: sin({deg}°) = {math.sin(rad)}")
elif choice == '10':
deg = get_number("Enter angle in degrees: ")
rad = math.radians(deg)
print(f"Result: cos({deg}°) = {math.cos(rad)}")
elif choice == '11':
deg = get_number("Enter angle in degrees: ")
rad = math.radians(deg)
try:
result = math.tan(rad)
print(f"Result: tan({deg}°) = {result}")
except:
print("Error: Tangent undefined at this angle.")
elif choice == '12':
x = get_number("Enter number to find factorial of: ")
if x < 0 or not x.is_integer():
print("Error: Factorial is defined for non-negative integers only.")
else:
print(f"Result: {int(x)}! = {math.factorial(int(x))}")
elif choice == '13':
deg = get_number("Enter angle in degrees: ")
print(f"Result: {deg}° = {math.radians(deg)} radians")
elif choice == '14':
rad = get_number("Enter angle in radians: ")
print(f"Result: {rad} radians = {math.degrees(rad)} degrees")
elif choice == '15':
print("Exiting calculator. Goodbye!")
break
else:
print("Invalid choice. Please select from 1 to 15.")
if __name__ == "__main__":
main()
How to Use:
- Copy the code to a file named
sci_calculator.py.
- Run it in your terminal or command prompt with:
python sci_calculator.py
- Follow the on-screen menu to choose operations.
Let me know if you want a graphical version or additional features!