viligen

math_calcs_eval_expression

Feb 10th, 2022
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.29 KB | None | 0 0
  1. def math_calcs(any_string):
  2.     num1, sign, num2 = any_string.split()
  3.     if sign == '^':
  4.         sign = '**'
  5.     try:
  6.         result = eval(f'{num1} {sign} {num2}')
  7.     except ZeroDivisionError:
  8.         return f'You can not divide by zero'
  9.     else:
  10.         return f'{result:.2f}'
  11.  
Advertisement
Add Comment
Please, Sign In to add comment