예제 #1
0
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)
예제 #2
0
def test_evaluate_value_error(expression):
    with pytest.raises(ValueError):
        evaluate(expression)
예제 #3
0
def test_evaluate_syntax_error(expression):
    with pytest.raises(SyntaxError):
        evaluate(expression)
예제 #4
0
def test_evaluate_name_error(expression):
    with pytest.raises(NameError):
        evaluate(expression)
예제 #5
0
def test_evaluate(expression, expected):
    assert evaluate(expression) == expected