what i did to day was make a calculator
import math
class AdvancedCalculator:
OPERATIONS = {
"+": lambda x, y: x + y,
"-": lambda x, y: x - y,
"*": lambda x, y: x * y,
"/": lambda x, y: x / y if y != 0 else "Error: Division by zero",
"^": lambda x, y: x ** y,
}
SCIENTIFIC_OPS = {
"sin": lambda x: math.sin(math.radians(x)),
"cos": lambda x: math.cos(math.radians(x)),
"tan": lambda x: math.tan(math.radians(x)),
"log": lambda x: math.log10(x) if x > 0 else "Error: Domain",
"sqrt": lambda x: math.sqrt(x) if x >= 0 else "Error: Negative sqrt",
}
def calculate_basic(self, n1, op, n2):
return self.OPERATIONS.get(op, lambda x, y: "Invalid Op")(n1, n2)
def calculate_scientific(self, op, n):
try:
return self.SCIENTIFIC_OPS.get(op, lambda x: "Invalid Op")(n)
except Exception:
return "Error"
def run(self):
print("--- Advanced Python Calculator ---")
print("Basic: +, -, *, /, ^ | Scientific: sin, cos, tan, log, sqrt")
while True:
choice = input("\nType 's' for scientific, 'b' for basic, or 'q' to quit: ").lower()
if choice == 'q':
break
try:
if choice == 'b':
n1 = float(input("First number: "))
op = input("Operation (+,-,*,/,^): ")
n2 = float(input("Second number: "))
print(f"Result: {self.calculate_basic(n1, op, n2)}")
elif choice == 's':
op = input("Function (sin, cos, tan, log, sqrt): ")
n = float(input("Value: "))
print(f"Result: {self.calculate_scientific(op, n)}")
except ValueError:
print("Invalid input. Please enter numbers only.")
if __name__ == "__main__":
calc = AdvancedCalculator()
calc.run()
Leave a Reply