Quick Dash Calculator 2021 Direct

import math def evaluate(expr: str) -> float | str: """ Quick Dash Calculator evaluator. Supports arithmetic, %, avg/sum, tip/split. """ expr = expr.strip().lower()

# Safe arithmetic using Python's eval (restricted) # In production, use a restricted environment or ast.literal_eval with operators. allowed_names = k: v for k, v in math.__dict__.items() if not k.startswith("__") allowed_names.update("abs": abs, "round": round) try: return eval(expr, {"__builtins__": {}}, allowed_names) except Exception: return "Error: invalid expression" if name == " main ": test_cases = [ "12*3.5+2", "15% of 200", "avg(4,8,12)", "tip(45.80,15)", "split(120,4)" ] for case in test_cases: print(f"case → evaluate(case)") quick dash calculator

# Handle percentages: "15% of 200" or "200 + 15%" if '% of' in expr: parts = expr.split('% of') percent = float(parts[0]) total = float(parts[1]) return percent / 100 * total import math def evaluate(expr: str) -> float |