Type-error in string checking

problems = ([“32 + 698”, “3801 * 2”])

for i in problems:
first, operator, second = i.split()


if operator not in [’+’, ‘-’]:
print (“Ok”)
print (“Error: Operator must be “+” or “-”.”)

This code gives Type error

Traceback (most recent call last):
File “./prog.py”, line 14, in
TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’`

It looks like you are trying to subtract two strings. Did you convert the strings representing the numbers into actual numbers?

Not to subtract

checking whether the string contains ‘+’ or ‘-’

The error

means that your code is trying to subtract two strings.

Is your code in the if/else blocks not indented in repl? That will also cause an error

Ah. This isn’t doing what you think. I think the quotes are actually closing your string and you are telling Python to print

"Error: Operator must be "
" or "

Which means Python is trying to add the first two parts and subtract the third. You need to escape the inner quote characters.

Solved :slightly_smiling_face: