-2

The input expression has to be in a + b and a - b form and the question asks to solve those equation.

(there is space before and after + and -)

So an example would be:

expression: 2.7 + 1000
1002.7

I only got to this point:

import math

text = input("expression: ")
bang21
  • 21
  • 2
  • This appears to be a homework or challenge question. What specific requirements do you have for valid solutions? What have you tried to actually evaluate the expression? You may also want to take a look at the [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems). – MisterMiyagi Jun 20 '20 at 06:53

3 Answers3

1

You can do this if we are guaranteed that the expression contains space before and after the "-" or "+"

import math

text = input("expression: ")

expression_list = text.split()

print(expression_list)

if expression_list[1] == '-':
    print(float(expression_list[0]) - float(expression_list[2]))

Similarly, you can do it for addition, multiplication and division as well.

Usama
  • 411
  • 4
  • 6
0

You can use the eval() method. This is a way of literally running expressions.

Shrey
  • 146
  • 2
  • 11
-1

One Simple solution for you.

import math
text = input("Expression: ")
output = eval(text)
print("Answer : ", output)

I have tested + - * / these four. try more. Read about eval Function

Vignesh
  • 1,553
  • 1
  • 10
  • 25