Arithmetic arranger, custom test cases working, replit giving errors

Hey guys, not sure where things are going wrong here, gave me errors, and so I double checked on VS code and the same test cases seemed to be working mostly.

Any insight is greatly appreciated! TIA

errors from replit:

 python main.py
   32      3801      45      123    
+ 698    -    2    + 43    +  49    
_____    ______    ____    _____    

======================== test session starts ========================
platform linux -- Python 3.8.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/runner/boilerplate-arithmetic-formatter
collected 10 items                                                  

test_module.py FFFFF..FFF                                     [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 '  3801      ...  _____    \n' == '  3801      ...----    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         + -    2    +  49    
E         ?                ++++
E         - ------    -----...
E         
E         ...Full output truncated (2 lines hidden), use '-vv' to show

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         ... ______    \n' == '  1         ...---    ------'
E         -   1         1
E         +   1         1    
E         ?              ++++
E         - + 2    - 9380
E         + + 2    - 9380    
E         ?              ++++
E         - ---    ------...
E         
E         ...Full output truncated (2 lines hidden), use '-vv' to show

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 '    3      3...  _____    \n' == '    3      3...----    -----'
E         -     3      3801      45      123
E         +     3      3801      45      123    
E         ?                                 ++++
E         - + 855    -    2    + 43    +  49
E         + + 855    -    2    + 43    +  49    
E         ?                                 ++++
E         - -----    ------    ----    -----...
E         
E         ...Full output truncated (2 lines hidden), use '-vv' to show

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 '  11      38... ______    \n' == '  11      38...---    ------'
E         -   11      3801      1      123         1
E         +   11      3801      1      123         1    
E         ?                                         ++++
E         - +  4    - 2999    + 2    +  49    - 9380
E         + +  4    - 2999    + 2    +  49    - 9380    
E         ?                                         ++++
E         - ----    ------    ---    -----    ------...
E         
E         ...Full output truncated (2 lines hidden), use '-vv' to show

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

arguments = [['44 + 815', '909 - 2', '45 + 43', '123 + 49', '888 + 40', '653 + 87']]
expected_output = 'Error: Too many problems.'
fail_message = 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."'

    @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 calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."
E       assert 'Error: Too many problems' == 'Error: Too many problems.'
E         - Error: Too many problems.
E         ?                         -
E         + Error: Too many problems

test_module.py:77: AssertionError
__________________ test_template[test_only_digits] __________________

arguments = [['98 + 3g5', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = 'Error: Numbers must only contain digits.'
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."'

    @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 calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."
E       assert 'Error: Numbe...ontain digits' == 'Error: Numbe...ntain digits.'
E         - Error: Numbers must only contain digits.
E         ?                                        -
E         + Error: Numbers must only contain digits

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 '    3      9...     1028    ' == '    3      9... 858     1028'
E         -     3      988
E         +     3      988    
E         ?               ++++
E         - + 855    +  40
E         + + 855    +  40    
E         ?               ++++
E         - -----    -----...
E         
E         ...Full output truncated (5 lines hidden), use '-vv' to show

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 '   32       ...     1028    ' == '   32       ... 172     1028'
E         -    32         1      45      123      988
E         +    32         1      45      123      988    
E         ?                                          ++++
E         - - 698    - 3801    + 43    +  49    +  40
E         + - 698    - 3801    + 43    +  49    +  40    
E         ?                                          ++++
E         - -----    ------    ----    -----    -----...
E         
E         ...Full output truncated (5 lines hidden), use '-vv' to show

test_module.py:77: AssertionError
====================== 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_too_many_problems] - Ass...
FAILED test_module.py::test_template[test_only_digits] - Assertion...
FAILED test_module.py::test_template[test_two_problems_with_solutions]
FAILED test_module.py::test_template[test_five_problems_with_solutions]
==================== 8 failed, 2 passed in 0.21s ====================

With the same test cases my code in VS code results in the below:

testcase 1:
  3801      123    
-    2    +  49    
------    -----    

testcase 2:
  1         1    
+ 2    - 9380    
---    ------    

testcase 3:
    3      3801      45      123            
+ 855    -    2    + 43    +  49            
-----    ------    ----    -----            

testcase 4:
  11      3801      1      123         1    
+  4    - 2999    + 2    +  49    - 9380    
----    ------    ---    -----    ------    

testcase 5:
Error: Too many problems
testcase 6:
Error: Operator must be '+' or '-'.
testcase 7:
Error: Numbers cannot be more than four digits.
testcase 8:
Error: Numbers must only contain digits
testcase 9:
    3      988
+ 855    +  40
-----    -----
  858     1028
testcase 10:
   32         1      45      123      988
- 698    - 3801    + 43    +  49    +  40
-----    ------    ----    -----    -----
 -666     -3800      88      172     1028

this is my code on replit:

def arithmetic_arranger(problems, answer=False):
  if len(problems) > 5:
    return "Error: Too many problems"
  first_operand = []
  second_operand = []
  operator = []

  for equation in problems:
    pieces = equation.split()  #split strings from lists, into pieces
    first_operand.append(pieces[0])  #add first operands to a list
    second_operand.append(pieces[2])  #add second operands to a list
    operator.append(pieces[1])  #add operators to a list

    if not all(numbers.isdigit() for (numbers) in first_operand) or not all(numbers.isdigit() for (numbers) in second_operand):
      return "Error: Numbers must only contain digits"
    if all(int(total) > 10000 for (total) in first_operand) or all(int(total) > 10000 for (total) in second_operand):
      return "Error: Numbers cannot be more than four digits."
  if '*' in operator or '/' in operator:
    return "Error: Operator must be '+' or '-'."

  first_row = []
  second_row = []
  third_row = []
  fourth_row = []
  line_length = None
  equation_spacing = '    '

  for i in range(len(problems)):
    if len(first_operand[i]) > len(second_operand[i]):
      length_difference = abs((len(first_operand[i]) - len(second_operand[i])))
      first_row.append('  ' + first_operand[i] + equation_spacing)
      second_row.append(operator[i] + ' ' + length_difference * ' ' + second_operand[i] + equation_spacing)

    else:
      length_difference = abs((len(first_operand[i]) - len(second_operand[i])))
      first_row.append(length_difference * ' ' + '  ' + first_operand[i] + equation_spacing)
      second_row.append(operator[i] + ' ' + second_operand[i] + equation_spacing)

    longest_operand = len((max(first_operand[i], second_operand[i], key=len)))
    line_length = longest_operand + 2
    third_row.append(line_length * '_' + equation_spacing)

    if answer:
      if operator[i] == '+':
        total = int(first_operand[i]) + int(second_operand[i])
        fourth_row.append(((line_length - len(str(total))) * ' ') + str(total) + equation_spacing)

      else:
        total = int(first_operand[i]) - int(second_operand[i])
        fourth_row.append(((line_length - len(str(total))) * ' ') + str(total) + equation_spacing)

    arranged_problems = ''.join(first_row) + "\n" + ''.join(second_row) + "\n" + ''.join(third_row) + "\n" + ''.join(fourth_row)
  return arranged_problems

This part is telling you that you have 4 extra spaces on the end of each line.

1 Like

in case it helps at this late stage: you’re also missing periods at the end of your Error messages

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