Trouble with Python Project - Arithmetic Arranger

Hello everyone. I am so close to completing my arithmetic arranger project, I am just failing 2 test. I genuinely don’t see what the issue could be considering that when I run the code in VS Code, I get the desired results but I do not get them in replit.

Here is my arithmetic_arranger.py file

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

    first_operand = []
    second_operand = []
    operator = []

    for problem in problems:
        element = problem.split()
        first_operand.append(element[0])
        second_operand.append(element [2])
        operator.append(element[1])
        
    if "*" in operator or "/" in operator:
        return "Error: Operator must be '+' or '-'."

    for i in range(len(first_operand)):
        if not (first_operand[i].isdigit() and second_operand[i].isdigit()):
            return "Error: Numbers must only contain digits."

    for i in range(len(first_operand)):
        if len(first_operand[i]) > 4 or len(second_operand[i]) > 4: 
            return "Error: Numbers cannot be more than four digits." 


    first_line = []
    second_line = []
    third_line = []
    fourth_line = []

    for x in range(len(first_operand)): 
        if len(first_operand[x]) > len(second_operand[x]) :
            first_line.append(" "*2 + first_operand[x])
        else: 
            first_line.append(" "*(len(second_operand[x]) - len(first_operand[x]) + 2) + first_operand[x])

    for i in range(len(second_operand)):
        if len(second_operand[i]) > len(first_operand[i]):
            second_line.append(operator[i] + " "+second_operand[i])
        else:
            second_line.append(operator[i]+ " "*(len(first_operand[i]) - len(second_operand[i]) + 1) + second_operand[i])
            
    for l in range(len(first_operand)):
        third_line.append("-"*(max(len(first_operand[l]), len(second_operand[l])) + 2))
        
    for i in range(len(first_operand)):
        if operator == "+" :
            ans = str(int(first_operand[i]) + int(second_operand[i]))
        else :
            ans = str(int(first_operand[i]) - int(second_operand[i]))
            
        if len(ans) > max(len(first_operand), len(first_operand)) :
            fourth_line.append(" " + ans)
        else :
            fourth_line.append(" "*max(len(first_operand[i]), len(first_operand[i]) - len(ans) + 2 ) + ans)
    
        arranged_problems = "    ".join(first_line) + "\n" + "    ".join(second_line) + "\n" + " ".join(third_line) + "\n" + " ".join(fourth_line) 
    else:
        arranged_problems = "    ".join(first_line) + "\n" + "    ".join(second_line) + "\n" + "    ".join(third_line)
        
    return arranged_problems

And here my main.py file

# This entrypoint file to be used in development. Start by reading README.md
from pytest import main

from arithmetic_arranger import arithmetic_arranger


print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))



# Run unit tests automatically
main()

And here is the error message I got from the console

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

test_module.py ........FF                                                [100%]

=================================== FAILURES ===================================
_______________ 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...----    -----' == '    3      9... 858     1028'
E         Skipping 33 identical leading characters in diff, use -v to show
E         - --    -----
E         ?            -
E         + --    -----
E         -   858     1028

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       ...----    -----' == '   32       ... 172     1028'
E         Skipping 114 identical leading characters in diff, use -v to show
E         - --    -----
E         ?            -
E         + --    -----
E         -  -666     -3800      88      172     1028

test_module.py:77: AssertionError
=========================== short test summary info ============================
FAILED test_module.py::test_template[test_two_problems_with_solutions] - Asse...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - Ass...
========================= 2 failed, 8 passed in 0.18s ==========================

I really appreciate any help or insight you all provide. I have been trying to debug this 2 days now and still can’t get it. May just need a new pair of eyes to interpret what is really going on.

You seem to be failing the problems where they want you to print the answer. I see you make answer=false in your code, but I don’t ever see in your code where you check to see if answer is true, and if so, that you are printing the answers.

1 Like

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