Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I compared the desired test output to my program output and everything seems correct. I removed trailing whitespace yet it still keeps failing. Can someone please help? thank you!

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    #check list size, return error if found
    if len(problems) > 5:
        return 'Error: Too many problems.'

    operand1_list = []
    operator_list = []
    operand2_list = []
    #optional lines_list = []
    #traverse each problem
    for problem in problems:
        #error checking
        problem_list = problem.split()
        if not (problem_list[1] == '+' or problem_list[1] == '-'):
            #wrong operator error
            return "Error: Operator must be '+' or '-'."
        
        if len(problem_list[0]) > 4 or len(problem_list[2]) > 4:
            #too many digits error
            return 'Error: Numbers cannot be more than four digits.'
        
        if not problem_list[0].isdigit() or not problem_list[2].isdigit():
            #wrong operand error
            return 'Error: Numbers must only contain digits.'
        
        #compile each first operand, operator, and second operand into respective
        #lists
        operand1_list.append(problem_list[0])
        operator_list.append(problem_list[1])
        operand2_list.append(problem_list[2])

    result = ' '
    for i in range(len(operand1_list)):
        #format first operand
        if len(operand1_list[i]) < len(operand2_list[i]):
            #num_spaces = max(len(operand1_list[i]), len(operand2_list[i]))-1
            #num_spaces = max(len(operand1_list[i]), len(operand2_list[i]))-1
            num_spaces = 1 + max(len(operand1_list[i]), len(operand2_list[i])) - len(operand1_list[i])
            result += num_spaces*' '
        else:
            result += ' '

        result += operand1_list[i] + '     '
        
        
    result += '\n'

    for i in range(len(operator_list)):
        #format operator and operand
        result+= operator_list[i] + ' '

        if len(operand1_list[i]) > len(operand2_list[i]):

            num_spaces = abs(len(operand1_list[i]) - len(operand2_list[i]))
            result += num_spaces*' '
        

        result += operand2_list[i] + '    '
    
    result += '\n'

    for i in range(len(operator_list)):
        #format lines
        num_lines = 2 + max(len(operand1_list[i]), len(operand2_list[i]))
        result += num_lines*'-' + '    '
        
    result += '\n'

    for i in range(len(operand1_list)):
        #format answers
        if show_answers == True:
            answer = int(operand1_list[i])
            if(operator_list[i] =='+'):
                answer += int(operand2_list[i])
            else:
                answer -= int(operand2_list[i])

            num_space = 2 + max(len(operand1_list[i]), len(operand2_list[i]))
            num_space -= len(str(answer))
            result += num_space*' ' + str(answer) + '    '

    return result.rstrip() #remove trailing whitespace and return


#tests

print(arithmetic_arranger(problems =["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], show_answers=True))
# print("\n   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028")


# print(arithmetic_arranger(problems =["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], show_answers=True))
# print("\n   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028")

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Welcome to the forum @realgajanandhongade

Your code:

3801      123     \n-    2    +  49    \n------    -----
3801      123\n-    2    +  49\n------    -----

Expected code above.

Happy coding

1 Like