-1

If the input is a string (ex. 2 + 2 = ), how can I make it to do the operation? I tried using eval, but that doesn't take the =, and also it doesn't print decimals. Also I tried split().

Thank you in advance,

Daniel
  • 3
  • 1
  • 2
  • remove the `=`. If you want to do it in a safe way you have to tell us what kind of operations and notations do you want to support. Do your operators have precedence, etc... – Karoly Horvath Oct 04 '15 at 17:19
  • 1
    http://stackoverflow.com/questions/594266/equation-parsing-in-python – Jason Oct 04 '15 at 17:20
  • it would be regular +, -, * and /. Thank you – Daniel Oct 04 '15 at 17:20
  • 1
    Show us code you tried and what the desired input and output are and what you got instead. No, we won't write your parser for you. – msw Oct 04 '15 at 17:30

1 Answers1

0

You can split at the '+' sign numbers=input.split('+'), then remove the '=' form the end, secondNumber=numbers.split('='). Afther that write

 if operator == '+':
   output = numbers[0] + secondNumber[0]
   print(output)

Or just output=eval(input.split('=')[0])

IamK
  • 2,753
  • 5
  • 30
  • 39