Arithmetic Formatter - Error keeps popping even though result is correct

Hello All,
I’ve been working on Arithmetic Arranger problem. Even though my output is exactly as it is desired. I’m still getting the following error. Any help would be extremely appreciated.

Error:

=============================== FAILURES ================================
_____________ test_template[test_two_problems_arrangement1] _____________

arguments = [['3801 - 2', '123 + 49']]
expected_output = '  3801      123\n-    2    +  49\n------    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert <function arithmetic_arranger at 0x7ff08a6feca0> == '  3801      123\n-    2    +  49\n------    -----'

test_module.py:77: AssertionError
------------------------- Captured stdout call --------------------------
  3801      123    
-    2    +  49    
------    -----    
_____________ test_template[test_two_problems_arrangement2] _____________

arguments = [['1 + 2', '1 - 9380']]
expected_output = '  1         1\n+ 2    - 9380\n---    ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]
E       assert <function arithmetic_arranger at 0x7ff08a6feca0> == '  1         1\n+ 2    - 9380\n---    ------'

test_module.py:77: AssertionError
------------------------- Captured stdout call --------------------------
  1         1    
+ 2    - 9380    
---    ------    
_____________ test_template[test_four_problems_arrangement] _____________

arguments = [['3 + 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = '    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
E       assert <function arithmetic_arranger at 0x7ff08a6feca0> == '    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----'

test_module.py:77: AssertionError
------------------------- Captured stdout call --------------------------
    3      3801      45      123    
+ 855    -    2    + 43    +  49    
-----    ------    ----    -----    
_____________ test_template[test_five_problems_arrangement] _____________

arguments = [['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380']]
expected_output = '  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]
E       assert <function arithmetic_arranger at 0x7ff08a6feca0> == '  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------'

test_module.py:77: AssertionError
------------------------- Captured stdout call --------------------------
  11      3801      1      123         1    
+  4    - 2999    + 2    +  49    - 9380    
----    ------    ---    -----    ------    
____________ test_template[test_two_problems_with_solutions] ____________

arguments = [['3 + 855', '988 + 40'], True]
expected_output = '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.
E       assert <function arithmetic_arranger at 0x7ff08a6feca0> == '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'

test_module.py:77: AssertionError
------------------------- Captured stdout call --------------------------
    3      988    
+ 855    +  40    
-----    -----    
  858     1028    
___________ test_template[test_five_problems_with_solutions] ____________

arguments = [['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True]
expected_output = '   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E       assert <function arithmetic_arranger at 0x7ff08a6feca0> == '   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028'

test_module.py:77: AssertionError
------------------------- Captured stdout call --------------------------
   32         1      45      123      988    
- 698    - 3801    + 43    +  49    +  40    
-----    ------    ----    -----    -----    
 -666     -3800      88      172     1028    
======================== short test summary info ========================
FAILED test_module.py::test_template[test_two_problems_arrangement1]
FAILED test_module.py::test_template[test_two_problems_arrangement2]
FAILED test_module.py::test_template[test_four_problems_arrangement]
FAILED test_module.py::test_template[test_five_problems_arrangement]
FAILED test_module.py::test_template[test_two_problems_with_solutions]
FAILED test_module.py::test_template[test_five_problems_with_solutions]
====================== 6 failed, 4 passed in 0.36s ======================
 

Your code so far

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

What function prints might be the same as the expected string, however function is not expected to print anything on it own. It should return string with arranged problems.

Hey, thank you so much for responding. I’m unable to rectify it as its not getting arranged. I’m still in the learning phase. Any help would be appreciated.

If you can post a link to your project code, we can give you better advice.

Thanks for responding. Please find the code below:

def arithmetic_arranger(mainlist,solutions=False):
  if len(mainlist)>5:
    return "Error: Too many problems."

#Variables assignment
  i=0 
  j=0
  x=0
  f=0
  s=0
  t=0
  l=0
  operator = ""
  resultlist = []
  newlist = []
  fl=''
  sl=''
  tl=''
  ll=''
  fll=[]
  sll=[]
  tll=[]
  lll=[]
  length1 = 0
  length2 = 0
  
#Splitting the main list
  for i in mainlist:
    newlist.append(i.split())
  
#Calculating the result and operator check
  for j in newlist:
    operator = j[1]
    if not ((operator == "+") or (operator == "-")):
      return("Error: Operator must be '+' or '-'.")
    while x<len(newlist):
      num1 = newlist[x][0]
      num2 = newlist[x][-1]
      length1 = len(num1)
      length2 = len(num2)
      if num1.isnumeric() == False:
        return('Error: Numbers must only contain digits.')
      if num2.isnumeric() == False:
        return ('Error: Numbers must only contain digits.')
      if (len(num1) > 4) or (len(num2) > 4) : 
        return ('Error: Numbers cannot be more than four digits.')
      if operator == '+':
        result = int(num1)+int(num2)
      elif operator == '-':
        result = int(num1)-int(num2)
      resultlist.append(result)

#Capturing the justified arrangement      
      if length1>length2:
        fl = str(num1).rjust(length1+2)
        fll.append(fl)
        sl = operator.rjust(1)+str(num2).rjust(length1+1)
        sll.append(sl)
        tl=('-'*length1)+'--'
        tll.append(tl)
        ll= str(resultlist[x]).rjust(length1+2)
        lll.append(ll) 
      else:
        fl = str(num1).rjust(length2+2)
        fll.append(fl)
        sl = operator.rjust(1)+str(num2).rjust(length2+1)
        sll.append(sl)
        tl=('-'*length2)+'--'
        tll.append(tl)
        ll= str(resultlist[x]).rjust(length2+2)
        lll.append(ll) 
              
      x=x+1
      break

#Printing the desired format  
  if solutions == True:
    for f in range(0,len(fll)):
      print(fll[f],end='    ')
    print('\n'.rstrip())
    for s in range(0,len(sll)):
      print(sll[s],end='    ')
    print('\n'.rstrip())
    for t in range(0,len(tll)):
      print(tll[t],end='    ')
    print('\n'.rstrip())
    for l in range(0,len(lll)):
      print(lll[l],end='    ')
  else:
    for f in range(0,len(fll)):
      print(fll[f],end='    ')
    print('\n'.rstrip())
    for s in range(0,len(sll)):
      print(sll[s],end='    ')
    print('\n'.rstrip())
    for t in range(0,len(tll)):
      print(tll[t],end='    ')
    print('\n'.rstrip())
 

Your function is supposed to return a string (not print anything). Do not use print function.

1 Like

I amended the code but its still displaying error.

Code:

def arithmetic_arranger(mainlist,answer=False):
  if len(mainlist)>5:
    return "Error: Too many problems."

#Variables assignment
  i=0 
  j=0
  x=0
  f=0
  s=0
  t=0
  l=0
  operator = ""
  resultlist = []
  newlist = []
  fl=''
  sl=''
  tl=''
  ll=''
  fll=[]
  sll=[]
  tll=[]
  lll=[]
  length1 = 0
  length2 = 0
  first = ''
  second = ''
  third = ''
  fourth = ''
  sol = ''
#Splitting the main list
  for i in mainlist:
    newlist.append(i.split())
  
#Calculating the result and operator check
  for j in newlist:
    operator = j[1]
    if not ((operator == "+") or (operator == "-")):
      return("Error: Operator must be '+' or '-'.")
    while x<len(newlist):
      num1 = newlist[x][0]
      num2 = newlist[x][-1]
      length1 = len(num1)
      length2 = len(num2)
      if num1.isnumeric() == False:
        return ('Error: Numbers must only contain digits.')
      if num2.isnumeric() == False:
        return ('Error: Numbers must only contain digits.')
      if (len(num1) > 4) or (len(num2) > 4) : 
        return ('Error: Numbers cannot be more than four digits.')
      if operator == '+':
        result = int(num1)+int(num2)
      elif operator == '-':
        result = int(num1)-int(num2)
      resultlist.append(result)

#Capturing the justified arrangement      
      if length1>length2:
        fl = str(num1).rjust(length1+2)
        fll.append(fl)
        sl = operator.rjust(1)+str(num2).rjust(length1+1)
        sll.append(sl)
        tl=('-'*length1)+'--'
        tll.append(tl)
        ll= str(resultlist[x]).rjust(length1+2)
        lll.append(ll) 
      else:
        fl = str(num1).rjust(length2+2)
        fll.append(fl)
        sl = operator.rjust(1)+str(num2).rjust(length2+1)
        sll.append(sl)
        tl=('-'*length2)+'--'
        tll.append(tl)
        ll= str(resultlist[x]).rjust(length2+2)
        lll.append(ll) 
              
      x=x+1
      break

#Printing the desired format  
  for f in range(0,len(fll)):
    first = fll[f]+'    '+first
  for s in range(0,len(sll)):
    second = sll[s]+'    '+second
  for t in range(0,len(tll)):
    third = tll[t]+'    '+third
  for l in range(0,len(lll)):
    fourth = lll[l]+'    '+fourth

  if answer==True:
    sol = first.rstrip() + "\n" + second.rstrip() + "\n" + third.rstrip() + "\n" + fourth.rstrip()
    return sol
  else:
    sol = first.rstrip() + "\n" + second.rstrip() + "\n" + third.rstrip()
    return sol
 

Error:

 python main.py
  123      45      3801       32
+  49    + 43    -    2    + 698
-----    ----    ------    -----
============================ test session starts ============================
platform linux -- Python 3.8.13, pytest-7.1.1, pluggy-1.0.0 -- /nix/store/06c55y0c5yzx5gx4l6k0pp6071zf1y5i-python3-3.8.13/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter
collected 10 items                                                          

test_module.py::test_template[test_two_problems_arrangement1] FAILED  [ 10%]
test_module.py::test_template[test_two_problems_arrangement2] FAILED  [ 20%]
test_module.py::test_template[test_four_problems_arrangement] FAILED  [ 30%]
test_module.py::test_template[test_five_problems_arrangement] FAILED  [ 40%]
test_module.py::test_template[test_too_many_problems] PASSED          [ 50%]
test_module.py::test_template[test_incorrect_operator] PASSED         [ 60%]
test_module.py::test_template[test_too_many_digits] PASSED            [ 70%]
test_module.py::test_template[test_only_digits] PASSED                [ 80%]
test_module.py::test_template[test_two_problems_with_solutions] FAILED [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] FAILED [100%]

================================= FAILURES ==================================
_______________ test_template[test_two_problems_arrangement1] _______________

arguments = [['3801 - 2', '123 + 49']]
expected_output = '  3801      123\n-    2    +  49\n------    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  123      3801\n+  49    -    2\n-----    ------' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         - -    2    +  49
E         - ------    -----
E         +   123      3801
E         + +  49    -    2
E         + -----    ------

test_module.py:77: AssertionError
_______________ test_template[test_two_problems_arrangement2] _______________

arguments = [['1 + 2', '1 - 9380']]
expected_output = '  1         1\n+ 2    - 9380\n---    ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]
E       assert '     1      1\n- 9380    + 2\n------    ---' == '  1         1\n+ 2    - 9380\n---    ------'
E         -   1         1
E         ?    ---
E         +      1      1
E         ? +++
E         - + 2    - 9380
E         - ---    ------
E         + - 9380    + 2
E         + ------    ---

test_module.py:77: AssertionError
_______________ test_template[test_four_problems_arrangement] _______________

arguments = [['3 + 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = '    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
E       assert ('  123      45      3801        3\n'\n '+  49    + 43    -    2    + 855\n'\n '-----    ----    ------    -----') == ('    3      3801      45      123\n'\n '+ 855    -    2    + 43    +  49\n'\n '-----    ------    ----    -----')
E         -     3      3801      45      123
E         - + 855    -    2    + 43    +  49
E         +   123      45      3801        3
E         + +  49    + 43    -    2    + 855
E         - -----    ------    ----    -----
E         ?                    --------
E         + -----    ----    ------    -----
E         ?      ++++++++

test_module.py:77: AssertionError
_______________ test_template[test_five_problems_arrangement] _______________

arguments = [['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380']]
expected_output = '  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]
E       assert ('     1      123      1      3801      11\n'\n '- 9380    +  49    + 2    - 2999    +  4\n'\n '------    -----    ---    ------    ----') == ('  11      3801      1      123         1\n'\n '+  4    - 2999    + 2    +  49    - 9380\n'\n '----    ------    ---    -----    ------')
E         -   11      3801      1      123         1
E         - +  4    - 2999    + 2    +  49    - 9380
E         - ----    ------    ---    -----    ------
E         +      1      123      1      3801      11
E         + - 9380    +  49    + 2    - 2999    +  4
E         + ------    -----    ---    ------    ----

test_module.py:77: AssertionError
______________ test_template[test_two_problems_with_solutions] ______________

arguments = [['3 + 855', '988 + 40'], True]
expected_output = '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.
E       assert '  988        3\n+  40    + 855\n-----    -----\n 1028      858' == '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
E         -     3      988
E         - + 855    +  40
E         +   988        3
E         + +  40    + 855
E           -----    -----
E         -   858     1028
E         +  1028      858

test_module.py:77: AssertionError
_____________ test_template[test_five_problems_with_solutions] ______________

arguments = [['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True]
expected_output = '   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E       assert ('  988      123      45         1       32\n'\n '+  40    +  49    + 43    - 3801    - 698\n'\n '-----    -----    ----    ------    -----\n'\n ' 1028      172      88     -3800     -666') == ('   32         1      45      123      988\n'\n '- 698    - 3801    + 43    +  49    +  40\n'\n '-----    ------    ----    -----    -----\n'\n ' -666     -3800      88      172     1028')
E         -    32         1      45      123      988
E         - - 698    - 3801    + 43    +  49    +  40
E         - -----    ------    ----    -----    -----
E         -  -666     -3800      88      172     1028
E         +   988      123      45         1       32
E         + +  40    +  49    + 43    - 3801    - 698
E         + -----    -----    ----    ------    -----
E         +  1028      172      88     -3800     -666

test_module.py:77: AssertionError
========================== short test summary info ==========================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - Ass...
FAILED test_module.py::test_template[test_two_problems_arrangement2] - Ass...
FAILED test_module.py::test_template[test_four_problems_arrangement] - Ass...
FAILED test_module.py::test_template[test_five_problems_arrangement] - Ass...
FAILED test_module.py::test_template[test_two_problems_with_solutions] - A...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - ...
======================== 6 failed, 4 passed in 0.37s ========================
 

Seems you’re code is presenting the equations in a backward order… the error output shows you whats expected vs what you provided, and it seems the order of the equations is backward (first output starts with 32-698, second starts with 988+40).

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

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

Thank you so much boss!
It works now. Thank you so so much. I’ve been working really hard on this.

Just changed:

  for f in range(0,len(fll)):
    first += fll[f]+'    '
  for s in range(0,len(sll)):
    second = sll[s]+'    '+second
  for t in range(0,len(tll)):
    third = tll[t]+'    '+third
  for l in range(0,len(lll)):
    fourth = lll[l]+'    '+fourth
  

What if I passed 10 problems to your function? What would happen? I would refactor your function to take it any number to make it more flexible and use less hard coding.

Actually I’ve written the code based on the following problem statement.

Problem Statement

I know the problem/challenge. I am just advising to refactor your code to make it more flexible (more useful). You have a lot of repeated code. Try making your code DRY. Also, to make your code more readable to others, you should try to use variable names that describe the data they represent.

Understood! Highly appreciate you’re valuable feedback. Will try to amend it and make it more dry and readable.
These advices are extremely helpful to a beginner like me. Thank you so much.