Another Arithmetic Formatter trouble

Hello Everyone,
I am also having trouble finding why the auto evaluator doesn’t pass my code.
It works perfectly when I run it locally, using the tests manually, but I get errors when displaying the results.

Do you have any ideas?

Thank you so much in advance.

This is my code:

def arithmetic_arranger (numbers, solve = False): firstRow ='' secondRow ='' thirdRow ='' resultRow =''
if len(numbers) <6:
    for operation in numbers:
        #split into pieces
        oper_split = operation.split()
        if len(oper_split[0])<5 and len(oper_split[2])<5:
            if oper_split[1] == '-' or oper_split[1] == '+':
                try:
                    result = int(oper_split[0]) + int(oper_split[2])
                except:
                    msg= 'Error: Numbers must only contain digits.'
                    return msg
                # Design the output in 7 rows and as many columns as needed
                # I need to know the length of the strings for the first and second numbers
                # The length of the operation is the longer number +2
                    
                len_1 = len(oper_split[0])
                len_2 = len(oper_split[2])
                if len_1 > len_2:
                    length = len_1 +2
                else:
                    length = len_2 +2
                        
                # First Row
                # Adding necessary space
                # Spaces Needed and put
                space_amount = length - len_1
                for i in range(space_amount):
                    firstRow +=" "
                # Put the number
                firstRow += str(oper_split[0])
                # Put the 4 spaces
                for i in range(4):
                    firstRow +=" "
                        
                # Second Row, The same using the operators as well
                secondRow += str(oper_split[1])
                # -1 is because we have an extra character, the operator
                space_amount = length - len_2 - 1
                for i in range(space_amount):
                    secondRow +=" "
                # Put the number
                secondRow += str(oper_split[2])
                # Put the 4 spaces
                for i in range(4):
                    secondRow +=" "
                        
                # Third Row is the ------- under the operation. It is length long
                for i in range(length):
                    thirdRow +="-"
                # Put the 4 spaces
                for i in range(4):
                    thirdRow +=" " 
                        
                #Result Row -the same
                space_amount = length - len(str(result))
                for i in range (space_amount):
                    resultRow += " "
                resultRow += str(result)
                for i in range(4):
                    resultRow += " "
                                        
            else:
                msg = 'Error: Operator must be \'+\' or \'-\'.'
                return msg
        else:
            msg = "Error: Numbers cannot be more than four digits."
            return msg
else:
    msg = "Error: Too many problems."
    return msg


msg = firstRow+'\n'+secondRow+'\n'+ thirdRow
if solve == True:
    msg += '\n'+ resultRow

return msg

The errors I receive are the following:

 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-5
collected 10 items

test_module.py FFFF…FF [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 … ----- ’ == ’ 3801 …---- -----’
E - 3801 123
E + 3801 123
E ? ++++
E - - 2 + 49
E + - 2 + 49
E ? ++++
E - ------ -----…
E
E …Full output truncated (3 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 … ------ ’ == ’ 1 …— ------’
E - 1 1
E + 1 1
E ? ++++
E - + 2 - 9380
E + + 2 - 9380
E ? ++++
E - — ------…
E
E …Full output truncated (3 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… ----- ’ == ’ 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 (3 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… ------ ’ == ’ 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 (3 lines hidden), use ‘-vv’ to show

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 (6 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] - Assert…
FAILED test_module.py::test_template[test_two_problems_arrangement2] - Assert…
FAILED test_module.py::test_template[test_four_problems_arrangement] - Assert…
FAILED test_module.py::test_template[test_five_problems_arrangement] - Assert…
FAILED test_module.py::test_template[test_two_problems_with_solutions] - Asse…
FAILED test_module.py::test_template[test_five_problems_with_solutions] - Ass…
========================= 6 failed, 4 passed in 0.12s ==========================

Next time include a link to the replit or make sure to format the error-messages as code. Because now the actual number of spaces is lost.
Anyway that looks like the standard error of having excess spaces at the end of each line.

1 Like

It was indeed that. At one point, where the 4 spaces after every number are made, they were also made for the last number, something not apparent at the result (as nothing would follow), but obviously necessary. This was fixed. I still get one error, but I think I can figure that out. Thank you so much for the insight! Have a great day.

1 Like

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