0

I want my program to do this:

  1. Input formula:

    (2*(3+4)-5)/6

  2. Output answer of formula: 1.5

Is there anyway to do it? I can't find anything from the Internet. Asking here is my last attempt.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
cpm pipes
  • 13
  • 2

1 Answers1

0

you can use eval

>>> a = eval('(2*(3+4)-5)/6')
>>> print(a)
1.5
Krishna
  • 924
  • 1
  • 7
  • 28
  • @cpm note that `eval` is a security risk, especially if you feed it user input. – mozway Mar 06 '22 at 06:58
  • Umm... What means "security risk"? Sorry but i dont understand – cpm pipes Mar 06 '22 at 07:00
  • If someone feeds arbitrary code (to delete files, perform network actions, etc.), it will be executed. You can read more [here](https://stackoverflow.com/questions/661084/security-of-pythons-eval-on-untrusted-strings) for example. – mozway Mar 06 '22 at 07:05
  • Thanks for pointing that security hole out. I have now patched my code so it exits if characters except numbers, +-*/ or . are found – cpm pipes Mar 06 '22 at 08:10