Arguments without quotes
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;
#!/usr/bin/env python3
from my_functions import *
print('')
print('Operator without quotes around it')
print('17.1 operator.add 9.1 is', round(get_truthWQ(17.1, operator.add, 9.1), 4))
print('17.1 operator.sub 9.1 is', round(get_truthWQ(17.1, operator.sub, 9.1), 4))
print('17.1 operator.mul 9.1 is', round(get_truthWQ(17.1, operator.mul, 9.1), 4))
print('17.1 operator.truediv 9.1 is', round(get_truthWQ(17.1, operator.truediv, 9.1), 4))
print('17.1 operator.floordiv 9.1 is', round(get_truthWQ(17.1, operator.floordiv, 9.1), 4))
print('')
print('Fractions')
print('17+1/3 operator.add 9+2/3 is', round(get_truthWQ(17+1/3, operator.add, 9+2/3), 4))
print('17+1/3 operator.sub 9+2/3 is', round(get_truthWQ(17+1/3, operator.sub, 9+2/3), 4))
print('17+1/3 operator.mul 9+2/3 is', round(get_truthWQ(17+1/3, operator.mul, 9+2/3), 4))
print('17+1/3 operator.truediv 9+2/3 is', round(get_truthWQ(17+1/3, operator.truediv, 9+2/3), 4))
print('17+1/3 operator.floordiv 9+2/3 is', round(get_truthWQ(17+1/3, operator.floordiv, 9+2/3), 4))
print('')
print('power of')
print('17.1 operator.gt 0.0 is', round(get_truthWQ(17.1, pow, 0.0), 4))
print('17.1 operator.gt 2 is', round(get_truthWQ(17.1, pow, 2), 4))
print('17.1 operator.gt 3 is', round(get_truthWQ(17.1, pow, 3), 4))
print('square root')
print('17.1 operator.gt 1/2 is', round(get_truthWQ(17.1, pow, 1/2), 4))
print('cube root')
print('17.1 operator.gt 1/3 is', round(get_truthWQ(17.1, pow, 1/3), 4))
print('')
print('true of false')
print('1.0 operator.gt 0.0 is', get_truthWQ(1.0, operator.gt, 0.0))
print('1.0 operator.lt 0.0 is', get_truthWQ(1.0, operator.lt, 0.0))
print('1.0 operator.ge 0.0 is', get_truthWQ(1.0, operator.ge, 0.0))
print('1.0 operator.le 0.0 is', get_truthWQ(1.0, operator.le, 0.0))
print('1.0 operator.eq 0.0 is', get_truthWQ(1.0, operator.eq, 0.0))
print('1.0 operator.ne 0.0 is', get_truthWQ(1.0, operator.ne, 0.0))
print('')
print('Fractions: true of false')
print('1/2 operator.gt 1/3 is', get_truthWQ(1/2, operator.gt, 1/3))
print('1/2 operator.lt 1/3 is', get_truthWQ(1/2, operator.lt,1/3))
print('1/2 operator.ge 1/3 is', get_truthWQ(1/2, operator.ge, 1/3))
print('1/2 operator.le 1/3 is', get_truthWQ(1/2, operator.le, 1/3))
print('1/2 operator.eq 1/3 is', get_truthWQ(1/2, operator.eq, 1/3))
print('1/2 operator.ne 1/3 is', get_truthWQ(1/2, operator.ne, 1/3))