python advanced 2026-11-24 结束

Calculator: evaluate an expression

Write evaluate(expr) that computes an arithmetic expression given as text, without eval, exec or ast.literal_eval.

  • Numbers can be integers or decimals (3, 2.5).
  • Operators + - * / with normal precedence, and parentheses.
  • Unary minus: -3, 2 * -(1 + 1).
  • Spaces may appear anywhere.
  • Raise ValueError for invalid input ("2 +", "(1", "4 $ 2") and ZeroDivisionError for division by zero.

Acceptance criteria

  • Return a float.
  • The tests at the bottom print All tests passed.

Hint: a tokenizer plus a recursive-descent parser (expression → term → factor) keeps this short.

目前已有 0 人解出。

python
编辑器Ctrl + Enter

            
        

当测试输出 "All tests passed" 时,此挑战会在本浏览器中标记为已解决。

提交您的解决方案进行审核