Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I can’t get pass this test. Please point out the problem for me.

Your code so far


def arithmetic_arranger(problems, show_answers=False):

    # Number/Operator contain list
    operator = []
    num_list = []
    final_num_list = []

    # Printing variable
    first_line = []
    second_line = []
    line_list = []
    sum_line = []

    # Too many problems
    if len(problems) > 5 :
        return('Error: Too many problems.')

    # Split part of problem
    for problem in problems:
        num = problem.split()
        operator.append(num[1])
        num_list.append(num[0])
        num_list.append(num[2])
        cross_line = ''
        dash = ' ' * 4

            # Digits check
        for number in num_list:
            if not number.isdigit():
                return('Error: Numbers must only contain digits.')

            # Digit width check
            elif len(number) > 4:
                return('Error: Numbers cannot be more than four digits.')

    # Calculation
        # Plus case
        if num[1] == '+':
            final_sum = int(num[0]) + int(num[2])

        # Minus case
        elif num[1] == '-':
            final_sum = int(num[0]) - int(num[2])

        legth = max(len(num[0]), len(num[2]))
        final_leght = legth + 2

        # Line maker
        for i in range(final_leght):
            cross_line += '-'

        # Make number even digit
            # First line
        if len(num[0]) < final_leght:
            for i in range(final_leght - len(num[0])):
                num[0] = ' ' + num[0]

            # Second line
        if len(num[2]) < final_leght :
            for i in range(final_leght - len(num[2]) - 1):
                num[2] = ' ' + num[2]
        
        num[2] = num[1] + num[2]
            
            # For sum
        if len(str(final_sum)) < final_leght :
            for i in range(final_leght - len(str(final_sum))):
                final_sum = ' ' + str(final_sum)
        

        # Containing data
        first_line.append(num[0] + dash)
        second_line.append(num[2] + dash)
        line_list.append(cross_line + dash)
        sum_line.append(final_sum + dash)

    # Check operator    
    for op in operator:
        if op in ['*', '/']:
            return("Error: Operator must be '+' or '-'.")

    if show_answers:
        answer = f"{''.join(first_line)}\n{''.join(second_line)}\n{''.join(line_list)}\n{''.join(sum_line)}"
    else:
        answer = f"{''.join(first_line)}\n{''.join(second_line)}\n{''.join(line_list)}"

    return answer

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

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Have you opened the browser console to check the more detaild output?

For example, the first line is yours and the second is the expected. Your line has extra spaces

AssertionError: '   3[32 chars]  988    \n- 698    - 3801    + 43    +  49   [100 chars]    '
             != '   3[32 chars]  988\n- 698    - 3801    + 43    +  49    +  [84 chars]1028'

This is an other way where you can see your output confronted with the desired one, the lines with - are yours and the lines with + are the expected ones, the lines with ? point out the differences

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

here too you have extra characters at the end of each line

2 Likes

Hey thanks to ILM response, I checked with F12 my code and basically my error was similar to yours.

Mine = '  3801      123(4 spaces)\n-    2    +  49(4 spaces)\n------    -----(4 spaces)' != 
Expected = '  3801      123\n-    2    +  49\n------    -----'

The thing is that they expect you to remove the last 4 spaces or ‘dash’ as per your code.
Similar to your code, I have added the 4 spaces at the end in a for loop to create each line.
To fix this I just removed the last 4 spaces from each line after the loop is completed.
something like first_line[:-4] and so on, and voila.