Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code is able to output the problems with answers; it looks identical to the output tests but I cannot pass them. I have accomplished the error tests, so I think it is a small detail I haven’t found.

Thanks in advance.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    #Operators go together with second_ops
    results_problems = {"first_ops":[], "second_ops":[], "dashes": [], "results": []}

    for index, problem in enumerate(problems):

        #Checks if there are more than 5 problems
        if len(problems) > 5:
            return "Error: Too many problems."

        #Splits problem from string
        first_op, operator, second_op = problem.split(" ")

        #Checks if operator is "+" or "-"
        if operator != "+" and operator != "-":
            return "Error: Operator must be '+' or '-'."

        #Checks if operand contains only digits
        if not first_op.isdigit() or not second_op.isdigit():
            return "Error: Numbers must only contain digits."

        #Checks if there are more than 4 digits
        if len(first_op) > 4 or len(second_op) > 4:
            return "Error: Numbers cannot be more than four digits."
            
        first_op_right_alig = 0
        second_op_right_alig = 0

        #right aligns and adds space between operand
        if len(first_op) > len(second_op):
            first_op =   "  " + first_op
            max_len = len(first_op)
            first_op = first_op.rjust(max_len)
            second_op = second_op.rjust(max_len - 1)
        elif len(first_op) < len(second_op):
            second_op =   " " + second_op
            max_len = len(second_op) + 1
            second_op = second_op.rjust(max_len - 1)
            first_op = first_op.rjust(max_len)
        else:
            first_op =   "  " + first_op
            second_op =   " " + second_op
            max_len = len(first_op) 

     
        #Creates dashes
        number_dashes = max_len
        dashes = ""
        for _ in range(number_dashes):
            dashes += "-"

        #Gets result of problem
        result = 0
        if operator == "+":
            result = int(first_op) + int(second_op)
        elif operator == "-":
            result = int(first_op) - int(second_op)
        
        result = str(result).rjust(max_len)

        #Adds space between previous and current problem
        spacing = ""
        if index != 0:
            spacing = "    "

        #Prepares the results problems dict to assemble final result        
        results_problems["first_ops"].append(spacing + first_op)
        results_problems["second_ops"].append(spacing + operator + second_op)
        results_problems["dashes"].append(spacing + dashes)
        if show_answers:
            results_problems["results"].append(spacing + result)

    #Assembles all parts horizontally
    final_result = ""
    if not show_answers:
        results_problems.pop("results")
    print(results_problems)
    
    for parts_problem in results_problems.values():            
        for part in parts_problem:
            final_result += part
        final_result += "\n"

    return final_result

print(f'\n{arithmetic_arranger(["3 + 855", "988 + 40"], True)}')
print("    3      988\n+ 855    +  40\n-----    -----\n  858     1028")

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0

Results

The result above is the text returned by my function; below, is the printed test.

Test with answers:
image

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Can you check the console and copy the results of the tests here?

// running tests

arithmetic_arranger(["3801 - 2", "123 + 49"])

should return

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

.

arithmetic_arranger(["1 + 2", "1 - 9380"])

should return

  1         1\n+ 2    - 9380\n---    ------

.

arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])

should return

    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----

.

arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])

should return

  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------

.

arithmetic_arranger(["3 + 855", "988 + 40"], True)

should return

    3      988\n+ 855    +  40\n-----    -----\n  858     1028

.

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

should return

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

. // tests completed

I think you need to check the browser console because it has more details than above. Do you know how to find it?

I was not able to find it; here is a picture of my console (I removed all prints except the default one at the bottom):

there should be a lot of messages in the console after clicking Run.

Can you make sure your browser console perspective shows all messages?
Then copy and paste the messages from there to here? (no screen shots pls)

Edit: you are filtering on the word Output so please remove the filter so you can see everything

I couldn’t figure it out in Firefox, so I switched to Google Chrome. Here is the console output:

13Third-party cookie will be blocked in future Chrome versions as part of Privacy Sandbox.Understand this warning
pyodide.asm.js:9 
pyodide.asm.js:9     3      988
pyodide.asm.js:9 + 855    +  40
pyodide.asm.js:9 -----    -----
pyodide.asm.js:9   858     1028
pyodide.asm.js:9 
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------    -----\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._pythonexc2js (pyodide.asm.js:9:654797)
    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)
ctx.onmessage @ python-test-evaluator.ts:177Understand this error
pyodide.asm.js:9 
pyodide.asm.js:9     3      988
pyodide.asm.js:9 + 855    +  40
pyodide.asm.js:9 -----    -----
pyodide.asm.js:9   858     1028
pyodide.asm.js:9 
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---    ------\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)
ctx.onmessage @ python-test-evaluator.ts:177Understand this error
pyodide.asm.js:9 
pyodide.asm.js:9     3      988
pyodide.asm.js:9 + 855    +  40
pyodide.asm.js:9 -----    -----
pyodide.asm.js:9   858     1028
pyodide.asm.js:9 
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: '    [36 chars]   -    2    + 43    +  49\n-----    ------    ----    -----\n' != '    [36 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)
ctx.onmessage @ python-test-evaluator.ts:177Understand this error
pyodide.asm.js:9 
pyodide.asm.js:9     3      988
pyodide.asm.js:9 + 855    +  40
pyodide.asm.js:9 -----    -----
pyodide.asm.js:9   858     1028
pyodide.asm.js:9 
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[60 chars]   +  49    - 9380\n----    ------    ---    -----    ------\n' != '  11[60 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)
ctx.onmessage @ python-test-evaluator.ts:177Understand this error
pyodide.asm.js:9 
pyodide.asm.js:9     3      988
pyodide.asm.js:9 + 855    +  40
pyodide.asm.js:9 -----    -----
pyodide.asm.js:9   858     1028
pyodide.asm.js:9 
pyodide.asm.js:9 
pyodide.asm.js:9     3      988
pyodide.asm.js:9 + 855    +  40
pyodide.asm.js:9 -----    -----
pyodide.asm.js:9   858     1028
pyodide.asm.js:9 
pyodide.asm.js:9 
pyodide.asm.js:9     3      988
pyodide.asm.js:9 + 855    +  40
pyodide.asm.js:9 -----    -----
pyodide.asm.js:9   858     1028
pyodide.asm.js:9 
pyodide.asm.js:9 
pyodide.asm.js:9     3      988
pyodide.asm.js:9 + 855    +  40
pyodide.asm.js:9 -----    -----
pyodide.asm.js:9   858     1028
pyodide.asm.js:9 
pyodide.asm.js:9 
pyodide.asm.js:9     3      988
pyodide.asm.js:9 + 855    +  40
pyodide.asm.js:9 -----    -----
pyodide.asm.js:9   858     1028
pyodide.asm.js:9 
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\n' != '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
      3      988
  + 855    +  40
  -----    -----
-   858     1028
?               -
+   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)
ctx.onmessage @ python-test-evaluator.ts:177Understand this error
pyodide.asm.js:9 
pyodide.asm.js:9     3      988
pyodide.asm.js:9 + 855    +  40
pyodide.asm.js:9 -----    -----
pyodide.asm.js:9   858     1028
pyodide.asm.js:9 
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[106 chars]   -----    -----\n -666     -3800      88      172     1028\n' != '   3[106 chars]   -----    -----\n -666     -3800      88      172     1028'
     32         1      45      123      988
  - 698    - 3801    + 43    +  49    +  40
  -----    ------    ----    -----    -----
-  -666     -3800      88      172     1028
?                                          -
+  -666     -3800      88      172     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)

this means you have an extra space character at the end of the dash line.
So try to find where that is coming from.
Edit: it could also be a newline or other invisible character

this one here though shows a space at the end of the totals line
Edit: it could also be a newline or other invisible character

1 Like

It was a newline at the end of the string.

I had

'    3      988\n+ 855    +  40\n-----    -----\n  858     1028\n'

When it had to be

'    3      988\n+ 855    +  40\n-----    -----\n  858     1028'

I passed, thanks for the help… I forgot I could use raw strings to check escape sequences =T

1 Like