lr5

parent 97c2b194
def check_types(op, allowed_types):
return type(op) in allowed_types
def calculate(op1, op2, act, allowed_types=(int, float, list, tuple)):
if not check_types(op1, allowed_types) and op1 is not None:
r=("ошибка в первом операнде")
elif not check_types(op2, allowed_types) and op2 is not None:
r=("ошибка во втором операнде")
else:
if type(op1) != float and type(op2) != float:
if type(op1) in (int,str):
op1=float(op1)
if type(op2) in (int,str):
op2=float(op2)
if (type(op1) is list and type(op2) is list)or (type(op1) is tuple and type(op2) is tuple):
if act == "+":
r = op1+op2
else:
print("Действие невозможно")
if type(op1) in (list, tuple) and op2==None:
if act=="+":
r=0
for i in op1:
r+=i
return r
else:
print("Действие невозможно")
pass
if act == "+":
r = op1 + op2
elif act == "-":
r = op1 - op2
elif act == "*":
r = op1 * op2
elif act == "/":
if op2!=0:
r = op1 / op2
else:
r="деление на ноль невозможно"
else:
r = "Операция не распознана"
return r
def main():
operand1 = float(input("Enter 1 value: "))
operand2 = float(input("Enter 2 value: "))
action = input("Enter type of operation: ")
result=(calculate(operand1, operand2, action))
print("Result is: " + str(result))
if __name__ == "__main__":
main()
print("running by interpreter")
\ No newline at end of file
PARAMS = {'precision': 0.00001, 'output_type': float, 'possible_types': (int, float)}
def convert_precision(prec):
''''
Преобразует точность данную в виде числа с плавающей точкой в целое число, возможное для использования с функцией round.
'''
prec=str(prec)
for i in prec:
if i=='e':
x=prec.split('-')
return int(x[1])
if i==".":
x=prec.split('.')
return int(len(x[1]))
def calculate(*args, **kwargs):
precision = convert_precision(kwargs['precision'])
output_type = kwargs['output_type']
print(precision)
if args[len(args) - 1] == '+':
result_sum = sum(args[0:len(args) - 1])
if type(result_sum) is not output_type:
result_sum = output_type(result_sum)
return result_sum
if args[len(args) - 1] == '-':
result_diff=0
for n in args[0:len(args) - 1]:
result_diff -= n
if type(result_diff) is not output_type:
result_sum = output_type(result_diff)
return result_diff
if args[len(args) - 1] == '*':
result_mult = 1
for n in args[0:len(args) - 1]:
result_mult *= n
if type(result_mult) is not output_type:
result_mult = output_type(result_mult)
return round(result_mult, precision)
if args[len(args) - 1] == '/':
result_division = args[0]
for n in args[1:len(args) - 1]:
result_division /= n
if type(result_division) is not output_type:
result_division = output_type(result_division)
return round(result_division, precision)
# print(calculate(*list(range(1, 10)), "+", **PARAMS)) # 1 + 2 + 3 + .. + 9
# print(calculate(*list(range(1, 10)), "-", **PARAMS))
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