1

I am trying to take a piece of user input: 5+5 or 5*5 but my code receives it as a string. I want to convert it to an expression from which an answer can be deduced. However, I am not sure how to do this.

Yash Sharma
  • 78
  • 1
  • 11

2 Answers2

1

For that there's a standard function called as eval.

Examples:

>>> eval("5*5")
25
>>> eval("5+5")
10
>>> eval("2+3*4-4")
10

It will take a string and then calculate the output as per the BODMAS Rule.

Keshav Biyani
  • 235
  • 1
  • 10
-1

Simply use "eval", but unsafe.

Radek Rojík
  • 104
  • 5