Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I finally finished this project and it gives the required output, however the tests are not approving this. I really cant see where I am going wrong. Please guide me.

Your code so far

import re


def arithmetic_arranger(problems, show_answers=True):
  
    topnum = ' '
    bottomnum = ' '
    lineup = ' '
    answer = ' '
    
    for problem in problems:

        #Errors
        if len(problems) > 5:
            return 'Error: Too many problems.'
        
        if (re.search('[/]', problem)) or (re.search('[*]', problem)):
                return "Error: Operator must be '+' or '-'."   

        if (re.search(r'[a-zA-Z]', problem)):
            return 'Error: Numbers must only contain digits.'

        first_num = problem.split()[0]
        operator = problem.split()[1]
        second_num = problem.split()[2]
        dashes = '-'
        if len(first_num) > 4 or len(second_num) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        #endofErrors

        result = ''
        
        if operator == '+':
            result = str(int(first_num) + int(second_num))
        elif operator == '-':
            result = str(int(first_num) - int(second_num))

        shift = max(len(first_num), len(second_num)) + 2
        top = first_num.rjust(shift)
        bottom = operator + second_num.rjust(shift -1)
        num_of_dashes = int(shift) 
        line = dashes.rjust(num_of_dashes, '-')
        reslut = result.rjust(shift)

        #excluding spaces in last char

        if problem != problems[-1]:
            topnum += top + '    '
            bottomnum += bottom + '    '
            lineup += line + '    '
            answer += reslut + '    '

        elif problem == problems [-1]:
            topnum += top
            bottomnum += bottom
            lineup += line
            answer += reslut

    if show_answers == False:
        final = topnum + '\n' + bottomnum + '\n' + lineup
    else:
        final = topnum + '\n' + bottomnum + '\n' + lineup + '\n' + answer

    return final


    #return problems


print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')


Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Welcome to the forum @1872star

arithmetic_arranger([“3801 - 2”, “123 + 49”]) should return

This is what your code is returning

Happy coding

1 Like

thank you thank you :)) (i put white spaces for no reason in the beginning variables)

1 Like