-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.py
More file actions
62 lines (48 loc) · 1.73 KB
/
Calculator.py
File metadata and controls
62 lines (48 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
def compute(operation, *args):
"""
Given two or more numbers compute its addition, or multiplication.
Given two numbers, compute addition, subtraction, multiplication or division.
Parameters:
x: Integer
y: Integer
operation: String. "add", "sub", "mul", "div"
Returns:
Result's computation. Float
"""
# --- Error handling ---
# Operation argument should be one of the following: "add", "sub", "mul" or "div"
if operation.lower() not in ["add", "sub", "mul", "div"]:
raise ValueError("Operation parameter should be one of the following add, sub, mul or div")
# Numbers should be all integers
if not all(isinstance(number, int) for number in args):
raise TypeError("args values should be all integer")
# --- Main ---
# Subtraction
if len(args) == 2 and operation.lower() == "sub":
x, y = args
print(x - y)
return float(x - y)
elif len(args) != 2 and operation.lower() == "sub":
raise ValueError("args should be two numbers")
# Division
if len(args) == 2 and operation.lower() == "div":
x, y = args
print(x / y)
return float(x / y)
elif len(args) != 2 and operation.lower() == "div":
raise ValueError("args should be two numbers")
# Addition
if len(args) > 1 and operation.lower() == "add":
res = sum(args)
print(res)
return float(res)
# Multiplication
if len(args) > 1 and operation.lower() == "mul":
res = np.prod(args)
print(res)
return float(res)
compute("add", 5,2,3)
compute("sub", 5,2)
compute("Mul", 5,2,3)
compute("div", 5,2)