Smart Calculator
Programming Basics · Python
TASK
Problem
Given `a op b`, where op is `+ - * / // %`. For `/`, `//`, `%` with b=0 print `ERROR`; otherwise compute.
EXAMPLE
Example
10 // 3
3
LIMITS
Constraints
op is valid
LEARN
Theory for this problem
+
`input()` reads data entered by the user. The result of `input()` is initially a string. `int()` converts a numeric string into an integer. `split()` separates one input line into individual values using spaces. The `+` operator adds numbers. The `-` operator subtracts one number from another. The `*` operator multiplies numbers. The `/` operator performs regular division and may return a decimal number. The `//` operator performs integer division and keeps the whole-number part. The `%` operator returns the remainder after division. Comparison operators `==`, `!=`, `<`, `>`, `<=`, `>=` compare values and produce `True` or `False`. `and` requires all combined conditions to be true. `or` requires at least one condition to be true. `if` checks a condition and runs its block when the condition is true. `else` runs when the corresponding `if` condition is false. `print()` displays the program result. Print only what the problem statement requires.
**Connection to this task.** The `//` and `%` operators complement each other: the first gives the integer quotient and the second gives the remainder. This is useful when splitting a number into groups or digits. The expression `'ERROR'` shows how this idea is applied to the task data.
NEED HELP?
Hints
+
['Handle division by zero first, then the operator.']
ANSWER
Solution
+
a, op, b = input().split()
a = int(a)
b = int(b)
if b == 0 and (op == '/' or op == '//' or op == '%'):
print('ERROR')
elif op == '+':
print(a + b)
elif op == '-':
print(a - b)
elif op == '*':
print(a * b)
elif op == '/':
print(a / b)
elif op == '//':
print(a // b)
else:
print(a % b)