lr5

parent 97c2b194
def check_types(op, allowed_types): PARAMS = {'precision': 0.00001, 'output_type': float, 'possible_types': (int, float)}
return type(op) in allowed_types
def calculate(op1, op2, act, allowed_types=(int, float, list, tuple)): def convert_precision(prec):
if not check_types(op1, allowed_types) and op1 is not None: ''''
r=("ошибка в первом операнде") Преобразует точность данную в виде числа с плавающей точкой в целое число, возможное для использования с функцией round.
elif not check_types(op2, allowed_types) and op2 is not None: '''
r=("ошибка во втором операнде") prec=str(prec)
else: for i in prec:
if type(op1) != float and type(op2) != float: if i=='e':
if type(op1) in (int,str): x=prec.split('-')
op1=float(op1) return int(x[1])
if type(op2) in (int,str): if i==".":
op2=float(op2) x=prec.split('.')
if (type(op1) is list and type(op2) is list)or (type(op1) is tuple and type(op2) is tuple): return int(len(x[1]))
if act == "+":
r = op1+op2 def calculate(*args, **kwargs):
else: precision = convert_precision(kwargs['precision'])
print("Действие невозможно") output_type = kwargs['output_type']
if type(op1) in (list, tuple) and op2==None: print(precision)
if act=="+":
r=0 if args[len(args) - 1] == '+':
for i in op1: result_sum = sum(args[0:len(args) - 1])
r+=i if type(result_sum) is not output_type:
return r result_sum = output_type(result_sum)
else: return result_sum
print("Действие невозможно")
pass if args[len(args) - 1] == '-':
if act == "+": result_diff=0
r = op1 + op2 for n in args[0:len(args) - 1]:
elif act == "-": result_diff -= n
r = op1 - op2 if type(result_diff) is not output_type:
elif act == "*": result_sum = output_type(result_diff)
r = op1 * op2 return result_diff
elif act == "/":
if op2!=0: if args[len(args) - 1] == '*':
r = op1 / op2 result_mult = 1
else: for n in args[0:len(args) - 1]:
r="деление на ноль невозможно" result_mult *= n
else: if type(result_mult) is not output_type:
r = "Операция не распознана" result_mult = output_type(result_mult)
return r return round(result_mult, precision)
if args[len(args) - 1] == '/':
def main(): result_division = args[0]
operand1 = float(input("Enter 1 value: ")) for n in args[1:len(args) - 1]:
operand2 = float(input("Enter 2 value: ")) result_division /= n
action = input("Enter type of operation: ") if type(result_division) is not output_type:
result=(calculate(operand1, operand2, action)) result_division = output_type(result_division)
print("Result is: " + str(result)) return round(result_division, precision)
if __name__ == "__main__":
main() # print(calculate(*list(range(1, 10)), "+", **PARAMS)) # 1 + 2 + 3 + .. + 9
print("running by interpreter") # print(calculate(*list(range(1, 10)), "-", **PARAMS))
\ No newline at end of file print(calculate(*list(range(1, 5)), "/", **PARAMS))
# print(calculate(*[1.0001, 2.2345], "*", **PARAMS)) # 2.23472345
def test_packed_calc_sum():
inp1, action = [1, 2, 3, 4, 5, 6, 7, 8, 9], "+"
assert calculate(*inp1, action,
**PARAMS) == 45.0, 'Sum of list of ints from 1 to 9'
assert type(calculate(*inp1, action, **
PARAMS)) is type(45.0), 'Type of sum is incorrect'
# test_packed_calc_sum()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment