Arithmetic Formatter string errors

Here is the code:

def arithmetic_arranger(problems):
#OK for first condition , length
if len(problems)>5:
print(“Error: Too many problems.”)

Spliting items

for cc in problems:
    it=cc.split()
    fi=it[0]
    se=it[1]
    th=it[2]
    #print(fi)

### 4 digits and cant be more than 4
    if len(fi)>4 or len(th)>4:
        print('Error: Numbers cannot be more than four digits.')
    if not fi.isnumeric() or not th.isnumeric():
        print('Error: Numbers must only contain digits.')
    if  se == '+' or se == '-':
        print('Error: Operator must be '+' or '-'.')

#arithmetic_arranger([‘1 + 22’])
arithmetic_arranger([‘33 2 22’,‘3 4 5’,‘55 - 5’])

Error message:
Traceback (most recent call last):
File “C:\Users\nrky1\Desktop\python new\ass1.py”, line 31, in
arithmetic_arranger([‘33 2 22’,‘3 4 5’,‘55 - 5’])
File “C:\Users\nrky1\Desktop\python new\ass1.py”, line 21, in arithmetic_arranger
print(‘Error: Operator must be ‘+’ or ‘-’.’)
TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’

I don’t quite understand why I would get this message as the elements in the list are strings.

Link to the problem set:

https://www.freecodecamp.dev/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter

Error indicates that string was attempted to be subtracted from another string. As such operation is not supported by str objects it resulted in TypeError.

Consider what is happening in line:

print('Error: Operator must be '+' or '-'.')

Notice there’s multiple single quotes used, as such each pair of them is considered as a one string:

'Error: Operator must be '
' or '
'.'

With + and - operators between them.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.