-1

Okay so Im very new to python and programming in general. I have input questions for the first and second number as well as the operation. This may sound dumb but Im having trouble with two things, when I'm printing the result for addition it combines the numbers. My other problem is that I have subtraction but it doesn't want to support the operand type.

The addition looks sum like this " (first_num+'+'+second_num+'='+(first_num) + (second_num))" The problem is it just combines the number say 5 + 3 = 53. Ive tried different things but cant come to a solution. The subtraction comes up with an error saying "unsupported operand type "-" for str and str. Im not exactly sure what to do with that and if it can subtract. Any clarity is thanked.

Murph
  • 1
  • 1
  • 1
    You're doing your "addition" and "subtraction" with strings rather than with numbers... You need to convert your strings to numbers before doing the math... Or you could just do an eval() of the string you create. So... int('2') + int('3') is 5. and eval('2+3') is also 5. – GaryMBloom Dec 20 '22 at 02:09
  • Strings and numbers are different types in most programming languages. The `+` operator means different things (concatenation vs. addition) for those types. – jarmod Dec 20 '22 at 02:09
  • 1
    Why don't you post your code instead of trying to explain how it is supposed to work in so many words? – Selcuk Dec 20 '22 at 02:16

1 Answers1

0

Looks like, you're performing operation on string.

Now, addition or multiplication works fine because, in python, strings will just get concatenated, but subtraction or division won't work on string.

Try typecasting the value to int or float, and it should work fine.

result = float(value1) - float(value2)