Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
I can’t see the difference between my output and the required output in the replit. I have checked the numbers and the spaces but I simply can’t see what I am missing with 10 out of 10 failed.

Your code so far

def arithmetic_arranger(problems, solution=None):

    l1 = []
    l2 = []
    l3 = []
    l4 = []
    validity = True

    if len(problems)>5:
        print("Error: Too many problems.",end="")

    else:
        for problem in problems:
            elements = problem.split()
            first_number = elements[0]
            operator = elements[1]
            second_number = elements[2]
            max_length = max(len(first_number), len(second_number)) + 2
            line1_space = max_length - len(first_number)
            line2_space = max_length - len(second_number) - 1

            if operator != "+" and operator != "-":
                validity = False
                print("Error: Operator must be '+' or '-'.",end="")
                break

            elif len(first_number)>4 or len(second_number)>4:
                validity = False
                print("Error: Numbers cannot be more than four digits.",end="")
                break

            elif not first_number.isdigit() or not second_number.isdigit():
                validity = False
                print("Error: Numbers must only contain digits.",end="")
                break

            else:
                for space1 in range(line1_space):
                    l1.append(' ')
                l1 = l1 + [first_number, "    "]

                l2.append(operator)
                for space2 in range(line2_space):
                    l2.append(' ')
                l2= l2 + [second_number, "    "]


                for dividers in range(max_length):
                    l3.append('-')
                l3.append("    ")

                if solution == True:
                    if operator == "+":
                        answer = int(first_number) + int(second_number)

                    else:
                        answer = int(first_number) - int(second_number)
                    line4_space = max_length - len(str(answer))
                    for space4 in range(line4_space):
                        l4.append(' ')
                    l4 = l4 + [answer, "    "]
    if validity == True and len(problems)<6:
        for items in l1:
            print(items, end="")
        print()

        for items in l2:
            print(items, end="")
        print()
        for items in l3:
            print(items, end="")
        
        if solution == True:
          print()
          for items in l4:
              print(items, end="")

Pytest results

   32      3801      45      123    
+ 698    -    2    + 43    +  49    
-----    ------    ----    -----    None
===================================================== test session starts =====================================================
platform linux -- Python 3.10.8, pytest-7.3.2, pluggy-1.0.0 -- /home/runner/boilerplate-arithmetic-formatter-1/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter-1
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] FAILED                                                            [ 50%]
test_module.py::test_template[test_incorrect_operator] FAILED                                                           [ 60%]
test_module.py::test_template[test_too_many_digits] FAILED                                                              [ 70%]
test_module.py::test_template[test_only_digits] FAILED                                                                  [ 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 None == '  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 None == '  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 None == '    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 None == '  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_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 None == 'Error: Too many problems.'

test_module.py:77: AssertionError
---------------------------------------------------- Captured stdout call -----------------------------------------------------
Error: Too many problems.
___________________________________________ 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)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."
E       assert None == "Error: Operator must be '+' or '-'."

test_module.py:77: AssertionError
---------------------------------------------------- Captured stdout call -----------------------------------------------------
Error: Operator must be '+' or '-'.
_____________________________________________ test_template[test_too_many_digits] _____________________________________________

arguments = [['24 + 85215', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = 'Error: Numbers cannot be more than four digits.'
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four 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 has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."
E       assert None == 'Error: Numbers cannot be more than four digits.'

test_module.py:77: AssertionError
---------------------------------------------------- Captured stdout call -----------------------------------------------------
Error: Numbers cannot be more than four digits.
_______________________________________________ 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 None == 'Error: Numbers must only contain digits.'

test_module.py:77: AssertionError
---------------------------------------------------- Captured stdout call -----------------------------------------------------
Error: Numbers must only contain digits.
_______________________________________ 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 None == '    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 None == '   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] - AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
FAILED test_module.py::test_template[test_two_problems_arrangement2] - AssertionError: Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]
FAILED test_module.py::test_template[test_four_problems_arrangement] - AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "12...
FAILED test_module.py::test_template[test_five_problems_arrangement] - AssertionError: Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "12...
FAILED test_module.py::test_template[test_too_many_problems] - AssertionError: Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."
FAILED test_module.py::test_template[test_incorrect_operator] - AssertionError: Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Opera...
FAILED test_module.py::test_template[test_too_many_digits] - AssertionError: Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Er...
FAILED test_module.py::test_template[test_only_digits] - AssertionError: Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to ...
FAILED test_module.py::test_template[test_two_problems_with_solutions] - AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arit...
===================================================== 10 failed in 0.26s ======================================================
 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

You need to parse the test output a bit. Here’s the first test:

Arguments = [[‘3801 - 2’, ‘123 + 49’]]
expected_output = ’ 3801 123\n- 2 + 49\n------ -----’

Here it shows your result None vs the expected output:

assert None == ’ 3801 123\n- 2 + 49\n------ -----’

The problem is that you are sending all your output to Print() statements, but it needs to be returned by the function using Return()

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