0

guys thanks for having me I've got a question already.

What I wanna do is to get sum of the list without for loop after splitting the given text by math symbols which the text as an example ** (1+3) * 3 ** should obtain the math priority for calculation.

my first question is how to get sum and sub and multiply or divide by the list and then how to check the priority and check it first.

# my calc

a = input()
result = a.split('+')

print(sum(result))


sol1: split brackets and mul /dev earlier sum /sub later but I know that split is not the best way!

sol2: make a tree I don t know what it is lol but mind it

it has answered here I know but with no split Calculator in python

1 Answers1

0

You could use eval (but be aware that it is usually a bad practice, see this answer):

result = eval(input())

If you input a string like (3-8)*4+5/2, the result will be automatically computed using normal priorities: -17.5.

Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50