Calculator
Programming Basics · Python
TASK
Problem
Given `a`, operator `op` (`+`, `-`, `*`, `/`), and `b`, print the result. Division always has `b != 0`.
EXAMPLE
Example
8 * 7
56.0
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. `float()` converts a numeric string into a number that may contain a decimal part. `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. Comparison operators `==`, `!=`, `<`, `>`, `<=`, `>=` compare values and produce `True` or `False`. `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.** An `if` statement chooses a program branch from a Boolean condition. Comparisons produce `True` or `False`, and `elif` lets you test several mutually exclusive cases in order. The expression `a + b` shows how this idea is applied to the task data.
NEED HELP?
Hints
+
['Use one branch for each operator.']
ANSWER
Solution
+
a, op, b = input().split()
a = float(a)
b = float(b)
if op == '+':
print(a + b)
elif op == '-':
print(a - b)
elif op == '*':
print(a * b)
else:
print(a / b)