Lesson 6.11
Let's build a calculator from functions — one function per operation:
def add(a, b):
return a + b
def sub(a, b):
return a - b
The function calc(a, op, b) picks the operation by the symbol:
def calc(a, op, b):
if op == "+":
return add(a, b)
...
The functions return the result with return, and calc decides whom to call.
The check sets two numbers x and y. Write add, sub, mul, div and calc
(for +, -, *, /) and print the result of three calls:
calc(x, "+", y);calc(x, "*", y);calc(x, "/", y).Each operation is a separate function with return.
calc picks a function by the symbol: if/elif and string comparisons == "+".
By default return subtraction. Call calc three times — +, *, /.