One Operator Calculator
Python
Medium
2 views
Problem Description
Expression is provided as: a op b (space-separated). op can be + - * / %. Division is integer division. If op is invalid or division by zero happens, output ERROR.
Input Format
One line: a op b.
Output Format
One line answer or ERROR.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<3:
sys.stdout.write('ERROR')
else:
try:
a=int(p[0]); op=p[1]; b=int(p[2])
if op=='+':
sys.stdout.write(str(a+b))
elif op=='-':
sys.stdout.write(str(a-b))
elif op=='*':
sys.stdout.write(str(a*b))
elif op=='/':
sys.stdout.write('ERROR' if b==0 else str(a//b))
elif op=='%':
sys.stdout.write('ERROR' if b==0 else str(a%b))
else:
sys.stdout.write('ERROR')
except Exception:
sys.stdout.write('ERROR')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!