Expression Calculator

Lab Python Advanced 30 points

Read expressions like 3 + 4, 10 / 4, 2 ^ 8 (one per line, first line n). Support + - * / ^. Print the result (drop .0 for whole results, else 2 decimals) or error for division by zero or an unknown operator.

Input

Line 1: n. Then n lines a op b.

Output

n lines.

Example

Input

5
3 + 4
10 / 4
2 ^ 8
1 / 0
5 % 2

Output

7
2.50
256
error
error
Sample input available Sample output available
Editor
Python

Hints

A dict from symbol to function avoids a long if/elif chain.

operator.add etc. are ready-made functions.

Solution explanation

Mapping symbols to functions is a dispatch table - easy to extend with new operators.

Show solution
import operator

OPS = {"+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv, "^": operator.pow}


def fmt(x):
    return str(int(x)) if float(x).is_integer() else f"{x:.2f}"


for _ in range(int(input())):
    a, op, b = input().split()
    try:
        print(fmt(OPS[op](float(a), float(b))))
    except (KeyError, ZeroDivisionError):
        print("error")
Output
Answer
HOME LEARN COMMUNITY DASHBOARD