Python Arithmetic arranger output seems correct but still runs into errors for all default cases

def arithmetic_arranger(problems, show = False):
    if len(problems) > 5:
        return "Error: Too many problems."
    def checkOperator(stringOperator):
        if stringOperator in ('-','+'): return True
    def checkDigit(stringDigit):
        if stringDigit.isdigit(): return True
    def checkLen(stringOperand):
        if len(stringOperand) <= 4: return True

    answer = ""
    fourWS = " "*4

    for operations in problems:#1st line
        currOperation = operations.split(' ') #[0] => first number [1] => operator [2] => last number
        maxSize = max(len(currOperation[0]), len(currOperation[2])) + 2
        if not checkOperator(currOperation[1]):
            return "Error: Operator must be '+' or '-'."
        if not (checkDigit(currOperation[0]) and checkDigit(currOperation[2])):
            return "Error: Numbers must only contain digits."
        if not (checkLen(currOperation[0]) and checkLen(currOperation[2])):
            return "Error: Numbers cannot be more than four digits."

        answer += currOperation[0].rjust(maxSize) + fourWS
    answer.rstrip()
    answer += "\n"

    for operations in problems:#2nd line
        currOperation = operations.split(' ') #[0] => first number [1] => operator [2] => last number
        maxSize = max(len(currOperation[0]), len(currOperation[2])) + 1
        answer += currOperation[1] + " " + currOperation[2].rjust(maxSize - 1) + fourWS

    answer.rstrip()
    answer += "\n"

    for operations in problems:#3rd line
        currOperation = operations.split(' ') #[0] => first number [1] => operator [2] => last number
        maxSize = max(len(currOperation[0]), len(currOperation[2])) + 2
        separator = maxSize * "-"
        answer += separator+fourWS
    answer.rstrip()
    answer += "\n"

    if show == True:
        for operations in problems:#4th line
            currOperation = operations.split(' ') #[0] => first number [1] => operator [2] => last number
            maxSize = max(len(currOperation[0]), len(currOperation[2])) + 2
            if currOperation[1] == '+':
                result = str(int(currOperation[0]) + int(currOperation[2]))
            else:
                result = str(int(currOperation[0]) - int(currOperation[2])) 
            answer += result.rjust(maxSize)+fourWS
    answer.rstrip()
    return answer
	

This is my function, and it when I try to run it through the tests it always fails the normal tests (those that don’t check for incorrect operators, letters instead of numbers etc)


I don’t understand why and I have been stuck on this for a while and it’s just frustrating because my output looks like what’s desired.

Please don’t post pictures ^^°
For the error, it’s hard to understand at first. But basically the error reads “-” is your output “+” is the expected output and “?” indicates differences.

The “?” line has “++++” at the end, indicating 4 trailing spaces (hence it looks right, because spaces are not visible).

Okay, thank you very much explaining the error notation, quickly realized my rstrip wasn’t doing anything so I had reassign with answer = answer.rstrip() and it worked

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