my_functions.py code
I placed all the arguments that will be called into one file called my_functions.py. I will be calling them into the other programs I am running.
get_truth() uses relative operators so that you can use either words or symbols for the math functions. it lists each keyword and value
math_func() uses if – elif statements. I also use {:.xf}”.format(result) to round the numbers where x is the precision
get_truthWQ() allows the operations in words without quotes. It is 2 lines.
#!/usr/bin/python3
import math
import operator
def get_truth(firstnum, op, secondnum):
rel_ops = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
'//': operator.floordiv,
'operator.add': operator.add,
'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);
def get_truthWQ(firstnumWQ=0, op=0, secondnumWQ=0):
return op(firstnumWQ, secondnumWQ);
def math_func(firstnum=0, operand=0, secondnum=0):
if operand == '*':
print(firstnum, '*', secondnum, '=', firstnum * secondnum)
return;
elif operand == '/':
result = firstnum / secondnum
if result - int(result) == 0:
print(firstnum, '/', secondnum, '=', int(result))
else:
print(firstnum, '/', secondnum, '=', "{:.4f}".format(result))
return;
elif operand == '+':
result = firstnum + secondnum
print(firstnum, '+', secondnum, '=', result)
return;
elif operand == '-':
result = firstnum - secondnum
print(firstnum, '-', secondnum, '=', result)
return;
elif operand == 'sqroot':
result = math.sqrt(firstnum)
if result - int(result) == 0:
print('square root of', firstnum, '=', int(result))
else:
print('square root of', firstnum, '=', "{:.4f}".format(result))
return;
elif operand == 'powerof':
from fractions import Fraction
result = math.pow(firstnum, secondnum)
# if the result returns an integer, it will only print the integer, else it will print 2 decimal places
# This also prints the fractional value not the decimal
if result - int(result) == 0:
print(firstnum, 'raised to the power of', Fraction(secondnum).limit_denominator(100), '=', int(result)
)
else:
print(firstnum, 'raised to the power of', Fraction(secondnum).limit_denominator(100),
'=', "{:.2f}".format(result))
return;
elif operand == 'fact':
result = math.factorial(firstnum)
print(firstnum, '! is =', result)
return;
elif operand == 'circarea':
result = math.pi * firstnum ** 2
print('area of circle with radius of', firstnum, 'is', "{:.4f}".format(result))
return;
elif operand == 'circcircum':
result = math.pi * 2 + firstnum
print('circumference of circle with radius of', firstnum, 'is', "{:.4f}".format(result))
return;
elif firstnum == operand == secondnum == 0:
print('you forgot the all the inputs')
return;
elif operand == secondnum == 0:
print('you forgot the operand')
return;
arguments_relops.py
You see the operand ‘+’ or ‘operator.add’ with quotes. Both return the same result.
get_truth(17, '+', 9)
get_truth(17, 'operator.add', 9)
arguments_relops.py code
#!/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.add 9 =', get_truth(17, 'operator.add', 9))
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))
I call the functions in
from my_functions import *
operator.add is not in quotes. I am rounding to 4 places.
round(get_truthWQ(17.1, operator.add, 9.1), 4))
The Output
Testing MDAS
17 + 9 = 26
17 - 9 = 8
17 * 9 = 153
17 / 9 = 1.8889
17 // 9 = 117 operator.add 9 = 26
17 operator.sub 9 = 8
17 operator.mul 9 = 153
17 operator.truediv 9 = 1.8889
17 operator.floordiv 9 = 1Testing 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 True1.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 TrueProcess finished with exit code 0