-3
import operator

ops = {
    '+' : operator.add,
    '-' : operator.sub,
    '*' : operator.mul,
    '/' : operator.truediv
}
n = input()
li = [n.split()]
li[0,2,4,6,8] = int(li[0,2,4,6,8])
li[1,3,5,7] = ops(li[1,3,5,7])
print(li)

I used this code above. I took help from Turn string into operator. So what I wanted to do was take in for example:

1 * 3 / 2 * 5 + 1

and split it into a list and then make the operators into operators and all the numbers integers. Then solve it as an equation.

1 Answers1

0

If you're looking to evaluate an expression you can use eval, which takes an infix expression (in the form of a string) and evaluates it (assuming it's a valid expression)

exp = input() # 4 + 3 * 2
answer = eval(exp) # 10
Eeshaan
  • 1,557
  • 1
  • 10
  • 22