Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code show the answer the same way they ask, but i dont undestand why dont pass on tests

Your code so far

def addSpaces(line_answer, answer_brute):
    line_split = line_answer.split()
    answer_split = answer_brute.split()
    i = 0
    answer_nums = ""

    for answers in answer_split:
        len_line = len(line_split[i])
        len_answer = len(answer_split[i])
        resp = len_line - len_answer

        if len_answer != len_line:
            answer_nums += answer_split[i].rjust(resp + len_answer) + "    "
        
        i += 1    

    return (answer_nums)


def arithmetic_arranger(problems, show_answers=False):

#-- Set limit of prblems ----------------
    if len(problems) > 5:
        return('Error: Too many problems.')

    line_Up = ""
    line_Down = ""
    line_Answer  = ""
    answer_brute = ""
#-- Add each Data of (problems) on (calc_unformated)-------------
    for problem in problems:
        calc = problem.split() 
        numUp = calc[0]
        operator = calc[1]
        numDown = calc[2]

#------ Conditions to invalid data 
        if operator == '*' or operator == '/':
            return "Error: Operator must be '+' or '-'."

        if not (numUp.isdigit() and numDown.isdigit()):
            return 'Error: Numbers must only contain digits.'

        if len(numUp) > 4 or len(numDown) > 4:
            return 'Error: Numbers cannot be more than four digits.'

#-- Operator Calc ---------------------------------
        if operator == '+':
            answer_brute += str(int(numUp) + int(numDown))
        else:
            answer_brute += str(int(numUp) - int(numDown))

        #print(len(answer))      

#------ Create the view style
        size = max(len(numUp), len(numDown)) + 2
        line_Up += numUp.rjust(size) + "    "
        line_Down += operator + numDown.rjust(size - 1) + "    "
        line_Answer += "-" * size + "    "
        answer_brute = answer_brute + "    "

    

    if show_answers == True:
        resolve = line_Up.rstrip() + "\n" + line_Down.rstrip() + "\n" + line_Answer.rstrip() + "\n" + addSpaces(line_Answer, answer_brute)
        
    else:
        resolve = line_Up.rstrip() + "\n" + line_Down.rstrip() + "\n" + line_Answer.rstrip()

    return resolve
print(f'\n{arithmetic_arranger(["3 + 855", "988 + 40"], True)}')

##Console Info:
// running tests 9.

arithmetic_arranger(["3 + 855", "988 + 40"], True)

should return

    3      988\n+ 855    +  40\n-----    -----\n  858     1028

. 10.

arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)

should return

   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028

. // tests completed

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

If you take a look at the browser’s console, there will be details regarding the failing test. You could also wrap the function call in repr function, like print(repr(...)), this will print string representation that might be easier to directly compare with the expected results, that are available by the tests.

thank you so much, on the code

###CODE
if len_answer != len_line:
answer_nums += answer_split[i].rjust(resp + len_answer) + " "

Missing a condition to the last item, i just can see this because of “repr”