def main(): """Main loop: Read and evaluate user's input.""" print(WELCOME) while True: # Read user's input try: expression = input(f"{PS1} ") except (KeyboardInterrupt, EOFError): raise SystemExit() # Handle special commands if expression.lower() == "help": print(USAGE) continue if expression.lower() in {"quit", "exit"}: raise SystemExit() # Evaluate the expression and handle errors try: result = evaluate(expression) except SyntaxError: # If the user enters an invalid expression print("Invalid input expression syntax") continue except (NameError, ValueError) as err: # If the user tries to use a name that isn't allowed print(err) continue # Print the result if no error occurs print(result)
def test_evaluate_value_error(expression): with pytest.raises(ValueError): evaluate(expression)
def test_evaluate_syntax_error(expression): with pytest.raises(SyntaxError): evaluate(expression)
def test_evaluate_name_error(expression): with pytest.raises(NameError): evaluate(expression)
def test_evaluate(expression, expected): assert evaluate(expression) == expected