title: python calculator which checks a sys.argv string for a + or -
body: <p>I'm trying to write a "simple" Python Calculator which evaluates an arithmatic expression. The expression is given as one string (one program argument) and consists of operands (positive integers) separated by operators (+ and -). The program evaluates the expression and prints out the result. When too many or too few arguments are provided, or the single program argument is malformed, the program prints "ERROR" (without quotes). some examples:</p>
  
  <pre><code>python calc.py 09-100-50
  -141
  
  python calc.py ""
  ERROR
  
  python calc.py 0+45+50-800+-5
  ERROR -&gt; gives error because there's a +- combination
  
  python calc.py -1
  ERROR
  
  python calc.py 1
  1
  </code></pre>
  
  <p>the thing is: I've got my code, but it doesn't add or substract the numbers, and how can I check if there's a +- combination</p>
  
  <pre><code>import sys
  t = len(sys.argv)
  num = ""
  new_num = 0
  if(t &gt; 2 or t == 1):
      print("ERROR")
      exit()
  s = sys.argv[1]
  new_num_f = 0
  if(sys.argv[1] == ""):
      print("ERROR")
      exit()
  if(s[0] == "-"):
      print("ERROR")
      exit()
  try:
      for i in range(len(s)):
          if(len(s) == 1):
              new_num = s[0]
          elif(str(s[i]) == "+"):
              new_num += int(num)
              num = ""
          elif(s[i] == "-"):
              new_num -= int(num)
              num = ""
          elif(str):
              num += s[i]
      new_num_f = new_num
  except(SyntaxError): #this is a wrong error, I don't know what error to use
      print("ERROR")
      exit()
  print(str(new_num_f))
  </code></pre>
  
  <p>if someone could tell me how to solve this and explain why to use it, thanks alot</p>