Build an Arithmetic Formatter Project - Failed test on formatting

Tell us what’s happening:

My code fails the first 4 and last 2 tests. It is weird because when I check them, they work fine. I have tried looking in the developer console but I have hit a wall. At this point I have spent a little less than half the time I spent on coding to debug this and I have absolutely given up. If anyone can nudge me in the right direction, I would really appreciate it. Apologies for having such a clunky and often redundant code. I am not sure where the formatting error is especially since I have personally check amount of spaces and other items. Again, I am not sure what the error is just that it makes my code fail the first 4 and last 2 tests.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    
    if len(problems) <= 5:
        list_of_values = []
        first_set_of_number = []
        second_set_of_number = []
        operator_holding_list = []
        length_of_first_operands = []
        length_of_second_operands = []
        number_of_dashes = []
        spaces_needed_for_top_operand = []
        spaces_needed_for_bottom_operand = []
        finalized_first_line = []
        finalized_second_line = []
        answers = []

        for item in problems:
            item = (item.replace(" ", "")).strip()
            temp_list_of_values = []

            # to validate input in terms of correct chars that is each character
            # must be a digit or + or -.
            # if not output/print a designated error message
            for char in item:

                if not char.isdigit():

                    if char == "+" or char == "-":
                        pass

                    elif char.isalpha():
                        return "Error: Numbers must only contain digits."
                        

                    else:
                        return "Error: Operator must be '+' or '-'."
                        

            # we add a desginated '!' at the end so we can terminate at desired
            # time when iterating in the below for loop
            item = item + "!"

            # using loop and conditional iterate through each char and seperate
            # into a temp_list, once the loop reaches a '+' or '-' it should
            # join temp_list and add it to the list_of_values than add the char
            # it was iterating to the list_of_values as well. Lastly, null the
            # temp_list and repeat
            # modified the designated last char in each item to "!" to ensure 
            # a '+' or '-' is not added to the list if not inputed by user
            for char in item:

                for value in list_of_values:
                    if len(value) > 4:
                        return "Error: Numbers cannot be more than four digits."
                        
                    
                if char.isdigit():
                    temp_list_of_values.append(char)

                elif char == "+" or char == "-":
                    list_of_values.append("".join(temp_list_of_values))
                    list_of_values.append(char)
                    temp_list_of_values = []

                else:
                    list_of_values.append("".join(temp_list_of_values))
                    temp_list_of_values = []
            # debug
                
            # print("Item in the input set seperated:", item)

        # print("\nThis is the list of values after items are seperated:", list_of_values)

        ## separate everything by category for ease of use later, i.e. first operand, second operand, and operator but also find min
        
        # check and get the first operand for each arthematic sequence in a seperate list
        for i in range(0, len(list_of_values), 3):
            first_set_of_number.append(list_of_values[i])
            length_of_first_operands.append(len(list_of_values[i]))
        # debug
        # print("\nThis is the list of operands that will go in the first print line:", first_set_of_number)

        # check and get the second operand for each arthimatic sequence in another separate list   
        for i in range(2, len(list_of_values), 3):
            second_set_of_number.append(list_of_values[i])
            length_of_second_operands.append(len(list_of_values[i]))
        # debug
        # print("\nThis is the list of the operands that will go on the second print line:", second_set_of_number)

        # get the operators in a seperate list for later use
        for i in range(1, len(list_of_values), 3):
            operator_holding_list.append(list_of_values[i])
        # debug
        # print("\nThese are the operators being used for each arthematic:", operator_holding_list)
                
        # print("\nLength of each value in the first line operands:", length_of_first_operands)
        # print("Length of each value of the second line operands:", length_of_second_operands)

        # because more works need to be done to get the actual number for how many dashes need to be placed so we will label this as such for it is the max when comparing the length between two operands from same problem
        max_length = []

        for i in range(len(length_of_second_operands)):
            largest_length = max(length_of_second_operands[i], length_of_first_operands[i])
            max_length.append(largest_length)
        
        # print("Compared set of values between first line and second line operand to find the one that is max:", max_length)


        for inst in range(len(max_length)):
            dashes_amount = max_length[inst] + 2
            number_of_dashes.append(dashes_amount)

            spaces_for_top = dashes_amount - length_of_first_operands[inst]
            spaces_needed_for_top_operand.append(spaces_for_top)

            spaces_for_bottom = dashes_amount - length_of_second_operands[inst] - 1
            spaces_needed_for_bottom_operand.append(spaces_for_bottom)

        # print("\nNumber of dashes to be placed for each arthematic:", number_of_dashes)
        # print("Number of spaces to leave for first operand:", spaces_needed_for_top_operand)
        # print("Number of spaces to leave between operator and operand on second line:", spaces_needed_for_bottom_operand)

        list_to_hold_amount_of_spaces_for_top_operands = []

        for each_amount_of_spaces in spaces_needed_for_top_operand:
            variable = ""

            for i in range(each_amount_of_spaces):
                variable += " "
            
            list_to_hold_amount_of_spaces_for_top_operands.append(variable)
            variable = ""
        
        for i in range(len(first_set_of_number)):
            if i != len(first_set_of_number) - 1:
                temp_holder = list_to_hold_amount_of_spaces_for_top_operands[i] + first_set_of_number[i] + "    "
                finalized_first_line.append(temp_holder)
            
            else:
                temp_holder = list_to_hold_amount_of_spaces_for_top_operands[i] + first_set_of_number[i]
                finalized_first_line.append(temp_holder)
            
        # print("\nFirst Line to be printed:", finalized_first_line)

        list_to_hold_amount_of_spaces_for_bottom_operands = []

        for each_amount_of_spaces in spaces_needed_for_bottom_operand:
            variable = ""

            for i in range(each_amount_of_spaces):
                variable += " "
            
            list_to_hold_amount_of_spaces_for_bottom_operands.append(variable)
            variable = ""        

        for i in range(len(second_set_of_number)):
            if i != len(second_set_of_number) - 1:
                temp_holder = list_to_hold_amount_of_spaces_for_bottom_operands[i] + second_set_of_number[i] + "    "
                temp_holder = operator_holding_list[i] + temp_holder
                finalized_second_line.append(temp_holder)

            else:
                temp_holder = list_to_hold_amount_of_spaces_for_bottom_operands[i] + second_set_of_number[i]
                temp_holder = operator_holding_list[i] + temp_holder
                finalized_second_line.append(temp_holder)
        
        # print("Second line to be printed:", finalized_second_line)

        list_to_hold_dashes = []

        for each in number_of_dashes:
            variable = ""

            for i in range(each):
                variable += "-"
            
            variable = variable + "    "
            list_to_hold_dashes.append(variable)
            variable = ""        
        
        # print("Third line to be printed:", list_to_hold_dashes, "\n")

    else:
        return "Error: Too many problems."

    if show_answers == False:
        first_line = "".join(finalized_first_line)
        second_line = "".join(finalized_second_line)
        third_line = "".join(list_to_hold_dashes)
    
        return f"{first_line}\n{second_line}\n{third_line}"
    else:
        for i in range(len(first_set_of_number)):
            variable = ""

            if operator_holding_list[i] == "+":
                variable = int(first_set_of_number[i]) + int(second_set_of_number[i])
                answers.append(variable)

            variable = ""

            if operator_holding_list[i] == "-":
                variable = int(first_set_of_number[i]) - int(second_set_of_number[i])
                answers.append(variable)
            
            variable = ""

        # print("Answers:", answers)

        answers_to_be_printed = []

        for i in range(len(answers)):
            spaces_needed = number_of_dashes[i] - len(str(answers[i]))
            variable = ""

            for each in range(spaces_needed):
                variable += " "

            if i != len(answers) - 1:
                variable = variable + str(answers[i]) + "    "
                answers_to_be_printed.append(variable)
            
            else:
                variable = variable + str(answers[i])
                answers_to_be_printed.append(variable)

            variable = ""

        # print("Fourth conditional line to be printed:", answers_to_be_printed)

        first_line = "".join(finalized_first_line)
        second_line = "".join(finalized_second_line)
        third_line = "".join(list_to_hold_dashes)    
        fourth_line = "".join(answers_to_be_printed)
        
        return f"{first_line}\n{second_line}\n{third_line}\n{fourth_line}"

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 Edg/126.0.0.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

can you copy and paste the output of the test here?

this is one of the output, I am now seeing there is an error of spacing which again I don’t understand or see when I do the tests myself.

yes that would be helpful.

i will leave the output from my test here as well
image

screenshots aren’t great for showing errors. But here’s what I see when I put your code in:



python-test-evaluator.ts:177 
PythonError: Traceback (most recent call last):
  File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
    .run(globals, locals)
     ^^^^^^^^^^^^^^^^^^^^
  File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
    coroutine = eval(self.code, globals, locals)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<exec>", line 4, in <module>
  File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "/lib/python311.zip/unittest/case.py", line 703, in fail
    raise self.failureException(msg)
AssertionError: '  3801      123\n-    2    +  49\n------    -----    ' != '  3801      123\n-    2    +  49\n------    -----'
    3801      123
  -    2    +  49
- ------    -----    ?                ----
+ ------    -----

    at new_error (pyodide.asm.js:9:14992)
    at pyodide.asm.wasm:0x152d67
    at pyodide.asm.wasm:0x152e6c
    at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
    at Module.callPyObject (pyodide.asm.js:9:75818)
    at Function.apply (pyodide.asm.js:9:89069)
    at Object.apply (pyodide.asm.js:9:87847)
    at Object.runPython (pyodide.asm.js:9:122345)
    at runPython (python-test-evaluator.ts:121:15)
    at test (eval at evaluatedTestString (python-test-evaluator.ts:90:13), <anonymous>:3:5)
    at ctx.onmessage (python-test-evaluator.ts:172:11)
python-test-evaluator.ts:177 
PythonError: Traceback (most recent call last):
  File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
    .run(globals, locals)
     ^^^^^^^^^^^^^^^^^^^^
  File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
    coroutine = eval(self.code, globals, locals)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<exec>", line 4, in <module>
  File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "/lib/python311.zip/unittest/case.py", line 703, in fail
    raise self.failureException(msg)
AssertionError: '  1         1\n+ 2    - 9380\n---    ------    ' != '  1         1\n+ 2    - 9380\n---    ------'
    1         1
  + 2    - 9380
- ---    ------    ?              ----
+ ---    ------

    at new_error (pyodide.asm.js:9:14992)
    at pyodide.asm.wasm:0x152d67
    at pyodide.asm.wasm:0x152e6c
    at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
    at Module.callPyObject (pyodide.asm.js:9:75818)
    at Function.apply (pyodide.asm.js:9:89069)
    at Object.apply (pyodide.asm.js:9:87847)
    at Object.runPython (pyodide.asm.js:9:122345)
    at runPython (python-test-evaluator.ts:121:15)
    at test (eval at evaluatedTestString (python-test-evaluator.ts:90:13), <anonymous>:3:5)
    at ctx.onmessage (python-test-evaluator.ts:172:11)
python-test-evaluator.ts:177 
PythonError: Traceback (most recent call last):
  File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
    .run(globals, locals)
     ^^^^^^^^^^^^^^^^^^^^
  File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
    coroutine = eval(self.code, globals, locals)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<exec>", line 4, in <module>
  File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "/lib/python311.zip/unittest/case.py", line 703, in fail
    raise self.failureException(msg)
AssertionError: '    [38 chars] -    2    + 43    +  49\n-----    ------    ----    -----    ' != '    [38 chars] -    2    + 43    +  49\n-----    ------    ----    -----'
      3      3801      45      123
  + 855    -    2    + 43    +  49
- -----    ------    ----    -----    ?                                 ----
+ -----    ------    ----    -----

    at new_error (pyodide.asm.js:9:14992)
    at pyodide.asm.wasm:0x152d67
    at pyodide.asm.wasm:0x152e6c
    at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
    at Module.callPyObject (pyodide.asm.js:9:75818)
    at Function.apply (pyodide.asm.js:9:89069)
    at Object.apply (pyodide.asm.js:9:87847)
    at Object.runPython (pyodide.asm.js:9:122345)
    at runPython (python-test-evaluator.ts:121:15)
    at test (eval at evaluatedTestString (python-test-evaluator.ts:90:13), <anonymous>:3:5)
    at ctx.onmessage (python-test-evaluator.ts:172:11)
python-test-evaluator.ts:177 
PythonError: Traceback (most recent call last):
  File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
    .run(globals, locals)
     ^^^^^^^^^^^^^^^^^^^^
  File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
    coroutine = eval(self.code, globals, locals)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<exec>", line 4, in <module>
  File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "/lib/python311.zip/unittest/case.py", line 703, in fail
    raise self.failureException(msg)
AssertionError: '  11[62 chars] +  49    - 9380\n----    ------    ---    -----    ------    ' != '  11[62 chars] +  49    - 9380\n----    ------    ---    -----    ------'
    11      3801      1      123         1
  +  4    - 2999    + 2    +  49    - 9380
- ----    ------    ---    -----    ------    ?                                         ----
+ ----    ------    ---    -----    ------

    at new_error (pyodide.asm.js:9:14992)
    at pyodide.asm.wasm:0x152d67
    at pyodide.asm.wasm:0x152e6c
    at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
    at Module.callPyObject (pyodide.asm.js:9:75818)
    at Function.apply (pyodide.asm.js:9:89069)
    at Object.apply (pyodide.asm.js:9:87847)
    at Object.runPython (pyodide.asm.js:9:122345)
    at runPython (python-test-evaluator.ts:121:15)
    at test (eval at evaluatedTestString (python-test-evaluator.ts:90:13), <anonymous>:3:5)
    at ctx.onmessage (python-test-evaluator.ts:172:11)
pyodide.asm.js:9 {first_line}
pyodide.asm.js:9 {second_line}
pyodide.asm.js:9 {third_line}
pyodide.asm.js:9 {fourth_line}
python-test-evaluator.ts:177 
PythonError: Traceback (most recent call last):
  File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
    .run(globals, locals)
     ^^^^^^^^^^^^^^^^^^^^
  File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
    coroutine = eval(self.code, globals, locals)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<exec>", line 4, in <module>
  File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "/lib/python311.zip/unittest/case.py", line 703, in fail
    raise self.failureException(msg)
AssertionError: '    3      988\n+ 855    +  40\n-----    -----    \n  858     1028' != '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
      3      988
  + 855    +  40
- -----    -----    
?               ----
+ -----    -----
    858     1028

    at new_error (pyodide.asm.js:9:14992)
    at pyodide.asm.wasm:0x152d67
    at pyodide.asm.wasm:0x152e6c
    at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
    at Module.callPyObject (pyodide.asm.js:9:75818)
    at Function.apply (pyodide.asm.js:9:89069)
    at Object.apply (pyodide.asm.js:9:87847)
    at Object.runPython (pyodide.asm.js:9:122345)
    at runPython (python-test-evaluator.ts:121:15)
    at test (eval at evaluatedTestString (python-test-evaluator.ts:90:13), <anonymous>:3:5)
    at ctx.onmessage (python-test-evaluator.ts:172:11)
pyodide.asm.js:9 {first_line}
pyodide.asm.js:9 {second_line}
pyodide.asm.js:9 {third_line}
pyodide.asm.js:9 {fourth_line}
python-test-evaluator.ts:177 
PythonError: Traceback (most recent call last):
  File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
    .run(globals, locals)
     ^^^^^^^^^^^^^^^^^^^^
  File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
    coroutine = eval(self.code, globals, locals)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<exec>", line 4, in <module>
  File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "/lib/python311.zip/unittest/case.py", line 703, in fail
    raise self.failureException(msg)
AssertionError: '   3[108 chars] -----    -----    \n -666     -3800      88      172     1028' != '   3[108 chars] -----    -----\n -666     -3800      88      172     1028'
     32         1      45      123      988
  - 698    - 3801    + 43    +  49    +  40
- -----    ------    ----    -----    -----    
?                                          ----
+ -----    ------    ----    -----    -----
   -666     -3800      88      172     1028

here’s one of the errors

If you’re not able to see the extra spaces then just add some visible characters instead of the spaces so that you can see them. (like use ! for the space characters and that will show you where they are)

i see, the empty spaces are resulting in the dashes print line and this issue is probably in my 2nd if statement since printing is done in that block. I’ll try to pinpoint where these spaces are coming from.
Though I have a question regarding the purple highlighted part. What is this? The yellow part are the spaces being printed what about the other one?
Screenshot 2024-07-12 173244

here’s an explanation for how to read the assertion results:

Yes this is really helpful. I didn’t knew what assertion error was. Now I understand and I was able to find the error. I fixed it by strip trailing whitespaces before returning the print line. I will keep in mind the technique to find whitespaces you mentioned. Once again, thanks!

1 Like

great work! I ran into the same problem myself when I solved this. It is a common issue.