tokens = ["+", "-", "/", "*", "^"] def parse(source): characters = list(source) final = [] for char in characters: if char.isdigit(): final.append(float(char)) else: for token in tokens: if char == token: final.append(token) break else: raise SyntaxError("Invalid character '" + char + "'") return final def evaluate(parsed): stack = [] for item in parsed: if isinstance(item, float) or (isinstance(item, str) and item.isdigit()): stack.append(float(item)) else: stack.append(item) # Initialize the result with the first number in the stack result = stack[0] for i in range(1, len(stack), 2): operator = stack[i] operand = stack[i + 1] if operator == "+": result += operand elif operator == "-": result -= operand elif operator == "/": result /= operand elif operator == "*": result *= operand elif operator == "^": result **= operand print(result) while True: source = input("> ") if not source: break try: parsed = parse(source) evaluate(parsed) except SyntaxError as e: print("Syntax Error: " + str(e)) except IndexError as e: print("Index Error: " + str(e)) except ValueError as e: print("Value Error: " + str(e))