Scientific Computing with Python Projects - Arithmetic Formatter

Hi,

I have gotten my code to work in another file by calling on the test values in the function.

The test looks incorrect to me because it is passing 5 problems in a test that is supposed to only have 2 and the same for the other which is making it fail the operator test.

When running the tests I get these 2 error messages:

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

test_module.py .....F..F.                            [100%]

========================= FAILURES =========================
__________ 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 '    3      3...---    ------' == "Error: Opera...e '+' or '-'."
E         - Error: Operator must be '+' or '-'.
E         +     3      3801      45      123         1
E         + / 855    -    2    + 43    +  49    - 9380
E         + -----    ------    ----    -----    ------

test_module.py:77: AssertionError
------------------- Captured stdout call -------------------
problem length:  4
op count : 0
op1 /
op2 -
op3 +
op4 +
op5 -
1
2
3
4
4
_____ 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...172     -9379' == '    3      9... 858     1028'
E         -     3      988
E         - + 855    +  40
E         - -----    -----
E         -   858     1028
E         +     3      988      45      123         1
E         + + 855    +  40    + 43    +  49    - 9380
E         + -----    -----    ----    -----    ------...
E         
E         ...Full output truncated (2 lines hidden), use '-vv' to show

test_module.py:77: AssertionError
------------------- Captured stdout call -------------------
problem length:  2
op count : 0
op1 +
op2 +
op3 +
op4 +
op5 -
1
2
3
4
5
5
================= short test summary info ==================
FAILED test_module.py::test_template[test_incorrect_operator]
FAILED test_module.py::test_template[test_two_problems_with_solutions]
=============== 2 failed, 8 passed in 0.19s ================




Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

The formatting may look weird posted here but it displays correctly in both my development file and when using pytest. Please help.

That’s caused by making pX variables global. Once one test sets p3 and p4, and further test doesn’t have that much problems for solving, the values won’t be replaced (or emptied) and both variables will have values from earlier test.

Generally that’s the exact reason why it’s best to avoid using global variables, unless it’s really necessary. It can have unexpected side effects, just like here :slight_smile:

You have all sorts of extra print statements in your code, possibly they were there for testing. You can try commenting those out. Otherwise I see what you mean.

The incorrect operator test fails, but when I put that input: [‘3 / 855’, ‘3801 - 2’, ‘45 + 43’, ‘123 + 49’] your program outputs:

problem length:  4
op count : 0
op1 /
op2 -
op3 +
op4 +
1
2
3
3
wrong operator
Error: Operator must be '+' or '-'.

Which is too much, it only needs to return the last sentence. You don’t need to print “wrong operator”. However the test script claims it returns a bunch of answers:

E     - Error: Operator must be '+' or '-'.
E     +     3      3801      45      123         1
E     + / 855    -    2    + 43    +  49    - 9380
E     + -----    ------    ----    -----    ------

It continues to print the problems, but only in the pytest. Same with the other fail, it prints the correct output, but then pytest says it prints some other problems. Very strange!

Would need to step through those tests and watch carefully.

I’m a bit suspicious of all your “try/except” statements in your code. Maybe you are just passing errors that you need to be fixing?

Yes, the printing was for testing. I’m still a newbie so my code needs lots of work. I don’t know how to assign variables dynamically in python so that was my work around. I’ll work on this more. Thank you for your help.

I was thinking that there was something related to memory but knew that wasn’t the case but this explains it. Thank you!

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