0

Help me find a way to calculate a string that contain numbers and + and -

Example : input : "2 +4 +7-9+1-3" Output: 2

17th Dec 2019, 8:46 PM
MohammadReza Yadollahi
MohammadReza Yadollahi - avatar
4 Réponses
+ 2
import re mystring = "2+4 +7-9+1-3" patten = r"[+-]?\d+" print(sum(int(x) for x in re.findall(patten, mystring)))
17th Dec 2019, 9:48 PM
rodwynnejones
rodwynnejones - avatar
17th Dec 2019, 9:53 PM
Petr
0
I want to write function like eval or sympy.sympify and dont want to use them
17th Dec 2019, 9:17 PM
MohammadReza Yadollahi
MohammadReza Yadollahi - avatar
0
print(eval("2 +4 +7-9+1-3"))
18th Dec 2019, 7:18 AM
Bilbo Baggins
Bilbo Baggins - avatar