Scientific Computing with Python Projects - Arithmetic Formatter

Hi,

I am having trouble seeing what is wrong with my output, it must be something obvious? thanks!

__________ 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      123\n-    2    +  49\n------    -----\n  3799      172' == '  3801      123\n-    2    +  49\n------    -----'
E             3801      123
E           -    2    +  49
E         - ------    -----
E         + ------    -----
E         ?                +
E         +   3799      172

test_module.py:77: AssertionError

i have edited your post for readability and moved it to its own topic.

you seem to have an extra space as indicated by the + symbol (at the end of the line that starts with the ?)
This means you have some kind of space probably after the dashes there at the end.
Try to find where that extra space is coming from as the test doesn’t like it.

Thanks, I see the problem is that I am not looking at the second optional parameter “display answers” so it is complaining about the ‘\n’ character. I haven’t found how to access this option param yet, any hints?

Can you post a link to the exercise because I don’t remember the details of it.

Here is a link to the excercise: https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter

I came up with a hack that seems to work, probably a better way to do it:

def arithmetic_arranger(problems):

for i in problems:
if i == True:
display_output = True
else:
display_output = False

I now pass 8/10 test cases, however I am getting a run time error with the test harness code below, seems like my function declaration is incorrect?

========================= 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)

E TypeError: arithmetic_arranger() takes 1 positional argument but 2 were given

test_module.py:76: TypeError
_____ 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)

E TypeError: arithmetic_arranger() takes 1 positional argument but 2 were given

test_module.py:76: TypeError

According to the instructions

The function should optionally take a second argument. When the second argument is set to True, the answers should be displayed.

So only display answers when the 2nd optional argument is given.

to define a optional argument just use a format similar to this sample code:
def helloWorld(name="World"):
Where the name here is the optional argument and it is by default set to World if not given.

Thanks, that did the trick! Hanaa, I really appreciate your timely help.

Mike

1 Like