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
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)))
+ 2
MohammadReza Yadollahi
for single-digit numbers only:
https://code.sololearn.com/cm9V4xL3M7A4/?ref=app
0
I want to write function like eval or sympy.sympify and dont want to use them
0
print(eval("2 +4 +7-9+1-3"))