Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I feel like my code should be correct but it doesn’t fulfill the required tests for the assignment.

Your code so far

final_result = []
def arithmetic_arranger(problems, show_answers=False):
    if len(problems) not in range(1,6):
        return ('Error: Too many problems.')
    else:
        first = ""
        second = ""
        lines = ""
        results = ""
        for problem in problems:
            first_space = problem.index(' ')
            first_int = problem[0:(first_space)]
            operator = problem[(first_space + 1)]
            second_int = problem[first_space + 3:]
            if operator == '+':
                result = str(int(first_int) + int(second_int))
            if operator == '-':
                result = str(int(first_int) - int(second_int))
            if operator == '/':
                return("Error: Operator must be '+' or '-'.")
            if operator == '*':
                return("Error: Operator must be '+' or '-'.")
            if len(first_int) > 4 or len(second_int) > 4:
                return 'Error: Numbers cannot be more than four digits.'
            if max(len(first_int),len(second_int)) == 4:
                upper = first_int.rjust(6) + "    "
                middle = operator + second_int.rjust(5) + "    "
                line = "------" + "    "
                first += upper
                second += middle
                lines += line
                results += result.rjust(6) + "    "
            elif max(len(first_int),len(second_int)) == 3:
                upper = first_int.rjust(5) + "    "
                middle = operator + second_int.rjust(4) + "    "
                line = "-----" + "    "
                first += upper
                second += middle
                lines += line
                results += result.rjust(5) + "    "
            elif max(len(first_int),len(second_int)) == 2:
                upper = first_int.rjust(4) + "    "
                middle = operator + second_int.rjust(3) + "    "
                line = "----" + "    "
                first += upper
                second += middle
                lines += line
                results += result.rjust(4) + "    "
            elif max(len(first_int),len(second_int)) == 1:
                upper = first_int.rjust(3) + "    "
                middle = operator + second_int.rjust(2) + "    "
                line = "---" + "    "
                first += upper
                second += middle
                lines += line
                results += result.rjust(3) + "    "
        if show_answers:
            return (f'{first}\n{second}\n{lines}\n{results}')
        else:   
            return (f'{first}\n{second}\n{lines}')


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


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

Hi there and welcome to our community!

For your print calls after your function, try putting the strings inside the repr() function, to see exactly how your strings are formatted in comparison to the expected output.

1 Like

Thanks a lot! I was able to fix the issue after realising what was wrong.

1 Like

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