-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathabc.py
More file actions
84 lines (70 loc) · 1.8 KB
/
mathabc.py
File metadata and controls
84 lines (70 loc) · 1.8 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'''A module to calculate numbers.
It takes function add、function minus、function multiplication、function divide、function pow、function floordiv and function delivery.'''
class InputError(BaseException):
'''Invalid input.'''
pass
def add(n1, n2):
'''print n1 + n2.'''
try:
n1 + n2
except Exception:
raise InputError from None
else:
print('%s + %s = %s' % (n1, n2, n1 + n2))
def minus(n1, n2):
'''print n1 - n2.'''
try:
n1 - n2
except Exception:
raise InputError from None
else:
print('%s - %s = %s' % (n1, n2, n1 - n2))
def multiplication(n1, n2):
'''print n1 * n2.'''
try:
n1 * n2
except Exception:
raise InputError from None
else:
print('%s * %s = %s' % (n1, n2, n1 * n2))
def divide(n1, n2):
'''print n1 / n2.'''
try:
n1 / n2
except Exception:
raise InputError from None
else:
if n1 / n2 == round(n1 / n2):
print('%s / %s = %s' % (n1, n2, round(n1 / n2)))
else:
print('%s / %s = %s ...... %s' % (n1, n2, round(n1 / n2), n1 - (round(n1 / n2) * n2)))
def pow(n1, b):
'''print n1 ** n2.'''
try:
n1 ** b
except Exception:
raise InputError from None
else:
print('%s ** %s = %s' % (n1, b, n1 ** b))
def floordiv(n1, n2):
'''print n1 // n2.'''
try:
n1 // n2
except Exception:
raise InputError from None
else:
print('%s // %s = %s' % (n1, n2, n1 // n2))
def delivery(n1, n2):
'''print n1 % n2.'''
try:
n1 % n2
except Exception:
raise InputError from None
else:
print(f'{n1} % {n2} = {n1 % n2}')
'''Test for mathabc.'''
def _test():
divide(15, 6)
divide(15, 'h')
if __name__ == "__main__":
_test()