import sys
for line in sys.stdin.read().splitlines():
left, op, right = line.split()
a, b = float(left), float(right)
if op in ("/", "//", "%") and b == 0:
result = "cannot divide by zero"
elif op == "+":
result = a + b
elif op == "-":
result = a - b
elif op == "*":
result = a * b
elif op == "/":
result = a / b
elif op == "//":
result = a // b
elif op == "%":
result = a % b
elif op == "**":
result = a ** b
else:
result = f"unknown operator {op}"
if isinstance(result, float) and result.is_integer():
result = int(result) # print 84 rather than 84.0
print(line, "=", result)
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!