• # Math calculator

Is their a better way of doing this? In my_function.py, I have the arguments mapped. The math program calls the print statements.

my_functions.py

``````#!/usr/bin/python3
import math
import operator

def get_truth(firstnum, op, secondnum):
rel_ops = {
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
'//': operator.floordiv,
'operator.sub': operator.sub,
'operator.mul': operator.mul,
'operator.truediv': operator.truediv,
'operator.floordiv': operator.floordiv,
'>': operator.gt,
'<': operator.lt,
'>=': operator.ge,
'<=': operator.le,
'==': operator.eq,
'!=': operator.ne,
'operator.gt': operator.gt,
'operator.lt': operator.lt,
'operator.ge': operator.ge,
'operator.le': operator.le,
'operator.eq': operator.eq,
'operator.ne': operator.ne
}
return rel_ops[op](firstnum, secondnum);

``````

math program

``````#!/usr/bin/env python3

from my_functions import *

print('Testing MDAS')
print('17 + 9 =', get_truth(17, '+', 9))
print('17 - 9 =', get_truth(17, '-', 9))
print('17 * 9 =', get_truth(17, '*', 9))
print('17 / 9 =', round(get_truth(17, '/', 9), 4))
print('17 // 9 =', round(get_truth(17, '//', 9), 4))
print('')
print('17 operator.sub 9 =', get_truth(17, 'operator.sub', 9))
print('17 operator.mul 9 =', get_truth(17, 'operator.mul', 9))
print('17 operator.truediv 9 =', round(get_truth(17, 'operator.truediv', 9), 4))
print('17 operator.floordiv 9 =', round(get_truth(17, 'operator.floordiv', 9), 4))
print('')
print('Testing truth or false')
print('1.0 > 0.0 is', get_truth(1.0, '>', 0.0))
print('1.0 < 0.0 is', get_truth(1.0, '<', 0.0))
print('1.0 >= 0.0 is', get_truth(1.0, '>=', 0.0))
print('1.0 <= 0.0 is', get_truth(1.0, '<=', 0.0))
print('1.0 == 0.0 is', get_truth(1.0, '==', 0.0))
print('1.0 != 0.0 is', get_truth(1.0, '!=', 0.0))
print('')
print('1.0 operator.gt 0.0 is', get_truth(1.0, 'operator.gt', 0.0))
print('1.0 operator.lt 0.0 is', get_truth(1.0, 'operator.lt', 0.0))
print('1.0 operator.ge 0.0 is', get_truth(1.0, 'operator.ge', 0.0))
print('1.0 operator.le 0.0 is', get_truth(1.0, 'operator.le', 0.0))
print('1.0 operator.eq 0.0 is', get_truth(1.0, 'operator.eq', 0.0))
print('1.0 operator.ne 0.0 is', get_truth(1.0, 'operator.ne', 0.0))

``````

The Output

``````/home/michael/PycharmProjects/Arguments/venv/bin/python /home/michael/PycharmProjects/Arguments/arguments_relops.py
Testing MDAS
17 + 9 = 26
17 - 9 = 8
17 * 9 = 153
17 / 9 = 1.8889
17 // 9 = 1

17 operator.sub 9 = 8
17 operator.mul 9 = 153
17 operator.truediv 9 = 1.8889
17 operator.floordiv 9 = 1

Testing truth or false
1.0 > 0.0 is True
1.0 < 0.0 is False
1.0 >= 0.0 is True
1.0 <= 0.0 is False
1.0 == 0.0 is False
1.0 != 0.0 is True

1.0 operator.gt 0.0 is True
1.0 operator.lt 0.0 is False
1.0 operator.ge 0.0 is True
1.0 operator.le 0.0 is False
1.0 operator.eq 0.0 is False
1.0 operator.ne 0.0 is True

Process finished with exit code 0

``````
• Are you looking to reduce the complexity of the dictionary in the function (thought it isn't that complex)?

• I usually like to tighten down coding as much as possible. I started creating a my_functions.py for all the functions as I go through the book. I started on class next in the intermediate book.

• I created a list and cut it down to one print statement. I could drop the other functions in my_functions.py.

``````#!/usr/bin/env python3

from my_functions import *
list = []
list = [[17, '+', 9], [17, '-', 9],  [17, '*', 9], [17, '/', 9], [17, '//', 9], [17, 'operator.add', 9],
[17, 'operator.sub', 9], [17, 'operator.mul', 9], [17, 'operator.truediv', 9], [17, 'operator.floordiv', 9],
[17, '>', 9], [17, '<', 9], [17, '>=', 9], [17, '<=', 9], [17, '==', 9], [17, '!=', 9], [17, 'operator.gt', 9],
[17, 'operator.lt', 9], [17, 'operator.ge', 9], [17, 'operator.le', 9], [17, 'operator.eq', 9],
[17, 'operator.ne', 9]
]

length = len(list)
print('Testing MDAS')
x = 0
while x < length:
print('')
if list[x][1] == '>':
print('')
print('Testing truth or false')
print(list[x][0], list[x][1], list[x][2], '=', get_truth(list[x][0], list[x][1], list[x][2]))
x += 1

``````
``````/home/michael/PycharmProjects/Arguments/venv/bin/python /home/michael/PycharmProjects/Arguments/arguments_relops.py
Testing MDAS
17 + 9 = 26
17 - 9 = 8
17 * 9 = 153
17 / 9 = 1.8888888888888888
17 // 9 = 1

17 operator.sub 9 = 8
17 operator.mul 9 = 153
17 operator.truediv 9 = 1.8888888888888888
17 operator.floordiv 9 = 1

Testing truth or false
17 > 9 = True
17 < 9 = False
17 >= 9 = True
17 <= 9 = False
17 == 9 = False
17 != 9 = True
17 operator.gt 9 = True
17 operator.lt 9 = False
17 operator.ge 9 = True
17 operator.le 9 = False
17 operator.eq 9 = False
17 operator.ne 9 = True

Process finished with exit code 0

``````
Posts 4Views 28