-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathfractions_operations.py
More file actions
34 lines (28 loc) · 942 Bytes
/
fractions_operations.py
File metadata and controls
34 lines (28 loc) · 942 Bytes
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
'''
fractions_operations.py
Fraction operations
'''
from fractions import Fraction
def add(a, b):
print('Result of adding {0} and {1} is {2} '.format(a, b, a+b))
def subtract(a, b):
print('Result of subtracting {1} from {0} is {2}'.format(a, b, a-b))
def divide(a, b):
print('Result of dividing {0} by {1} is {2}'.format(a, b, a/b))
def multiply(a, b):
print('Result of multiplying {0} and {1} is {2}'.format(a, b, a*b))
if __name__ == '__main__':
try:
a = Fraction(input('Enter first fraction: '))
b = Fraction(input('Enter second fraction: '))
op = input('Operation to perform - Add, Subtract, Divide, Multiply: ')
if op == 'Add':
add(a, b)
if op == 'Subtract':
subtract(a, b)
if op == 'Divide':
divide(a, b)
if op == 'Multiply':
multiply(a, b)
except ValueError:
print('Invalid fraction entered')