The test cases aren't being passed for the arithmetic formatter1

Hello, I have finished my artithentic formatter but the test cases are not being passed. I am viewing the console for mistakes but it is kind of difficult to read what I did wrong. Could someone guide me to the right direction? Thanks.

first_operand_list = [];
second_operand_list = [];
line_list = [];
result_list = [];
def arithmetic_arranger(problems, result = False):
    def format_answers():
        i = 2;
        j = 2;
        k = 1;
        while i < len(first_operand_list) and j < len(second_operand_list):
            for n in result_list[k]:
                if n == "-":
                    result_list[k - 1] = ' ';
                    break;
                else:
                    result_list[k - 1] = '  ';
            
                if (len(result_list[k])) == 4:
                    for n in result_list[k]:
                        if n == '-':
                            result_list[k - 1] = ' ';
                            break;
                        else:
                            result_list[k - 1] = ' ';
        
            i += 3;
            j += 3;
            k += 3;


    def to_answer():
        i = 2;
        j = 2;
        while i < len(first_operand_list) and j < len(second_operand_list):
            for k in second_operand_list[j - 1]:
                if k == '-':
                    subtract = int(first_operand_list[i]) - int(second_operand_list[j]);
                    result_list.append('  ');
                    result_list.append(str(subtract));
                    result_list.append('    ');
                elif k == '+': 
                    add = int(first_operand_list[i]) + int(second_operand_list[j]);
                    result_list.append('  ');
                    result_list.append(str(add));
                    result_list.append('    ');
            i += 3;
            j += 3;
        format_answers();
        
    def to_third_row():
        i = 2;
        j = 2;
        while i < len(first_operand_list) and j < len(second_operand_list):
            if len(first_operand_list[i]) < len(second_operand_list[j]):
                lines = '';
                for k in range(0, len(second_operand_list[j]) + 2):
                    lines += '-';
                line_list.append(lines);
                line_list.append('    ');
            elif len(first_operand_list[i]) > len(second_operand_list[j]):
                lines = '';
                for k in range(0, len(first_operand_list[i]) + 2):
                    lines += '-';
                line_list.append(lines);
                line_list.append('    ');
            elif len(first_operand_list[i]) == len(second_operand_list[j]):
                lines = '';
                for k in range(0, len(first_operand_list[i]) + 2):
                    lines += '-';
                line_list.append(lines);
                line_list.append('    ');
            i += 3;
            j += 3;

        if result:
            to_answer();

    def format_spaces():
        i = 2;
        j = 2;
        while i < len(first_operand_list) and j < len(second_operand_list):
            if len(first_operand_list[i]) == 1:
                if len(second_operand_list[j]) == 1:
                    first_operand_list[i - 1] = '  ';
                elif len(second_operand_list[j]) == 2:
                    first_operand_list[i - 1] = '   ';
                elif len(second_operand_list[j]) == 3:
                    first_operand_list[i - 1] = '    ';
                elif len(second_operand_list[j]) == 4:
                    first_operand_list[i - 1] = '     ';
            elif len(first_operand_list[i]) == 2:
                if len(second_operand_list[j]) == 1:
                    first_operand_list[i - 1] = '  ';
                elif len(second_operand_list[j]) == 2:
                    first_operand_list[i - 1] = '  ';
                elif len(second_operand_list[j]) == 3:
                    first_operand_list[i - 1] = '   ';
                elif len(second_operand_list[j]) == 4:
                    first_operand_list[i - 1] = '    ';
            elif len(first_operand_list[i]) == 3:
                if len(second_operand_list[j]) == 1:
                    first_operand_list[i - 1] = '  ';
                elif len(second_operand_list[j]) == 2:
                    first_operand_list[i - 1] = '  ';
                elif len(second_operand_list[j]) == 3:
                    first_operand_list[i - 1] = '  ';
                elif len(second_operand_list[j]) == 4:
                    first_operand_list[i - 1] = '   ';
            elif len(first_operand_list[i]) == 4:
                if len(second_operand_list[j]) == 1:
                    first_operand_list[i - 1] = '  ';
                elif len(second_operand_list[j]) == 2:
                    first_operand_list[i - 1] = '  ';
                elif len(second_operand_list[j]) == 3:
                    first_operand_list[i - 1] = '  ';
                elif len(second_operand_list[j]) == 4:
                    first_operand_list[i - 1] = '  ';
            i += 3;
            j += 3;

        to_third_row();

    def format_operators():
        i = 2;
        j = 2;
        second_operand_list[0] = '';
        first_operand_list[0] = '';
        while i < len(first_operand_list) and j < len(second_operand_list):
            if (len(first_operand_list[i]) < len(second_operand_list[j])):
                second_operand_list[j - 1] = second_operand_list[j - 1] + ' ';
                
            elif (len(first_operand_list[i]) > len(second_operand_list[j])):
                larger_spaces = '';
                if (len(second_operand_list[j])) == 3:
                    second_operand_list[j - 1] = second_operand_list[j - 1] + '  ';
                elif (len(second_operand_list[j])) == 1:
                    for k in range(0, len(first_operand_list[i])):
                        larger_spaces += ' ';
                    first_operand_list[i - 1] = '    ';
                    second_operand_list[j - 1] = second_operand_list[j - 1] + larger_spaces;
                else:
                    for k in range(0, len(first_operand_list[i]) - 1):
                        larger_spaces += ' ';
                    first_operand_list[i - 1] = '    ';
                    second_operand_list[j - 1] = second_operand_list[j - 1] + larger_spaces;
            else:
                 second_operand_list[j - 1] = second_operand_list[j - 1] + ' ';
            i += 3;
            j += 3;
        
        format_spaces();

    def to_first_row(first_operand):
        first_operand_list.append('    ');
        first_operand_list.append(' ');
        first_operand_list.append(str(first_operand));
        if (len(first_operand_list) == len(problems) * 3) and (len(second_operand_list) == len(problems) * 3):
            format_operators();

    def to_second_row(second_operand, operator):
        second_operand_list.append('    ');
        second_operand_list.append(operator);
        second_operand_list.append(str(second_operand));
        if (len(first_operand_list) == len(problems) * 3) and (len(second_operand_list) == len(problems) * 3):
            format_operators();
        
    if len(problems) > 5:
        return 'Error: Too many problems.';

    for i in problems:
        for j in i:
            if j == '*' or j == '/':
                return 'Error: Operator must be '+' or '-'.';

    for k in problems:
        k = k.split(' ');
        for u in k:
            if len(u) > 4 and len(u) > 4:
                return 'Error: Numbers cannot be more than four digits.';
            else:
                try:
                    first_operand = int(k[0]);
                    operator = k[1];
                    second_operand = int(k[2]);
                    to_first_row(first_operand);
                    to_second_row(second_operand, operator);
                except:
                    return 'Error: Numbers must only contain digits.';

    if result:
        return "".join(first_operand_list) + '\n' + "".join(second_operand_list) + '\n' + "".join(line_list) + '\n' + "".join(result_list);
    else:
        return "".join(first_operand_list) + '\n' + "".join(second_operand_list) + '\n' + "".join(line_list);
    
print(arithmetic_arranger(['24 + 85215', '3801 - 2', '45 + 43', '123 + 49']));
   

The error messages:

=================================== 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 ' 24     24  ...    -----    ' == '  3801      ...----    -----'
E         -   3801      123
E         - -    2    +  49
E         - ------    -----
E         +  24     24       32       32     32     3801     3801     3801     45     45     45     123     123     123     3801     3801     3801     123     123     123
E         + + 85215    + 85215    + 698    + 698    +698    -2    -2    -2    +43    +43    +43    +49    +49    +49    -2    -2    -2    +49    +49    +49
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 ' 24     24  ...    -----    ' == '  1         ...---    ------'
E         -   1         1
E         - + 2    - 9380
E         - ---    ------
E         +  24     24       32       32     32     3801     3801     3801     45     45     45     123     123     123     3801     3801     3801     123     123     123     1     1     1     1     1     1
E         + + 85215    + 85215    + 698    + 698    +698    -2    -2    -2    +43    +43    +43    +49    +49    +49    -2    -2    -2    +49    +49    +49    +2    +2    +2    -9380    -9380    -9380
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 ' 24     24  ...    -----    ' == '    3      3...----    -----'
E         -     3      3801      45      123
E         - + 855    -    2    + 43    +  49
E         - -----    ------    ----    -----
E         +  24     24       32       32     32     3801     3801     3801     45     45     45     123     123     123     3801     3801     3801     123     123     123     1     1     1     1     1     1     3     3     3     3801     3801     3801     45     45     45     123     123     123
E         + + 85215    + 85215    + 698    + 698    +698    -2    -2    -2    +43    +43    +43    +49    +49    +49    -2    -2    -2    +49    +49    +49    +2    +2    +2    -9380    -9380    -93...
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 ' 24     24  ...    -----    ' == '  11      38...---    ------'
E         -   11      3801      1      123         1
E         - +  4    - 2999    + 2    +  49    - 9380
E         - ----    ------    ---    -----    ------
E         +  24     24       32       32     32     3801     3801     3801     45     45     45     123     123     123     3801     3801     3801     123     123     123     1     1     1     1     1     1     3     3     3     3801     3801     3801     45     45     45     123     123     123     11     11     11     3801     3801     3801     1     1     1     123     123     123     1     1     1
E         + + 85215    + 85215    + 698    + 698    +698    -2    ...
E         
E         ...Full output truncated (2 lines hidden), use '-vv' to show

test_module.py:77: AssertionError
____________________ test_template[test_incorrect_operator] ____________________

arguments = [['3 / 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = "Error: Operator must be '+' or '-'."
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be \'+\' or \'-\'."'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
>       actual = arithmetic_arranger(*arguments)

test_module.py:76:

   TypeError: unsupported operand type(s) for -: 'str' and 'str'

arithmetic_arranger.py:173: TypeError
_______________ 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 ' 24     24  ...  -----    \n' == '    3      9... 858     1028'
E         +  24     24       32       32     32     3801     3801     3801     45     45     45     123     123     123     3801     3801     3801     123     123     123     1     1     1     1     1     1     3     3     3     3801     3801     3801     45     45     45     123     123     123     11     11     11     3801     3801     3801     1     1     1     123     123     123     1     1     1     24     24     3     3     3     988     988     988
E         + + 85215    + 85215    + 698    + 698    +698    -2    -2    -2    +43    +43    +43    +49    +49    +49    -2    -2    -2  ...
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 ' 24     24  ...  -----    \n' == '   32       ... 172     1028'
E         +  24     24       32       32     32     3801     3801     3801     45     45     45     123     123     123     3801     3801     3801     123     123     123     1     1     1     1     1     1     3     3     3     3801     3801     3801     45     45     45     123     123     123     11     11     11     3801     3801     3801     1     1     1     123     123     123     1     1     1     24     24     3     3     3     988     988     988     32     32     32     1     1     1     45     45     45     123     123     123     988     988     988
E         + + 85215    + 852...
E         
E         ...Full output truncated (6 lines hidden), use '-vv' to show

test_module.py:77: AssertionError

It would help a bunch if you shared your error messages. Thanks.

1 Like

Okay, I think I got them.

Do you have the full error messages? The truncated version isn’t very helpful.

One problem I see off the top of my head is that you need to return strings, not print them.

1 Like

I have removed the print statements in my code, but it appears to have no effect. Are these the error messages?

Did you replace those prints with returns?

It is much easier to follow the conversation if you post updates to your code instead of editing each time.

1 Like

It looks like your logic for building the output is all mixed up. You are putting a lot of duplicate info in here.

That must have been the last print statement I think. But the only thing I am returning is this:


    if result:
        return "".join(first_operand_list) + '\n' + "".join(second_operand_list) + '\n' + "".join(line_list) + '\n' + "".join(result_list);
    else:
        return "".join(first_operand_list) + '\n' + "".join(second_operand_list) + '\n' + "".join(line_list);

Just found out I am returning multiple values. It wasn’t like this before.

You only return from a function once. It is literally impossible that returning from the same function multiple times is your issue.

I meant that it returned more than the desired result. You helped me figure that out, thanks for that. Although, it is weird that it did not happen before.

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