# Problems with the arithmetic formatter

the code:

import re

def arithmetic_arranger(problems, solve = False):

if(len(problems) > 5):
return “Error: Too many problems”

first = “”
second = “”
lines = “”
sumx = “”
string = “”
for problem in problems:
if(re.search("[^\s0-9.±]", problem)):
if(re.search("[/]", problem) or re.search("[*]", problem)):
return “Error: Operator must be ‘+’ or ‘-’.”
return “Error: Numbers must only contain digits.”

``````  firstNumber = problem.split(" ")[0]
operator = problem.split(" ")[1]
secondNumber = problem.split(" ")[2]

if(len(firstNumber) >= 5 or len(secondNumber) >= 5):
return "Error: Numbers cannot be more than four digits."

sum = ""
if(operator == "+"):
sum = str(int(firstNumber) + int(secondNumber))
elif(operator == "-"):
sum = str(int(firstNumber) - int(secondNumber))

length = max(len(firstNumber), len(secondNumber)) + 2
top = str(firstNumber).rjust(length)
bottom = operator + str(secondNumber).rjust(length - 1)
line = ""
res = str(sum).rjust(length)
for s in range (length):
line += "-"

if problem != problems[-1]:
first += top + '    '
second += bottom + '    '
lines += line + '    '
sumx += res + '    '
else:
first += top
second += bottom
lines += line
sumx += res

if solve:
string = first + "\n" + second + "\n" + lines + "\n" + sumx
else:
string = first + "\n" + second + "\n" + lines
return string
``````

the error: ============================= 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-2
collected 10 items

test_module.py FFFFF.F.FF [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 ‘\n\n’ == ’ 3801 …---- -----’
E Strings contain only whitespace, escaping them using repr()
E - ’ 3801 123\n- 2 + 49\n------ -----’
E + ‘\n\n’

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 ‘\n\n’ == ’ 1 …— ------’
E Strings contain only whitespace, escaping them using repr()
E - ’ 1 1\n+ 2 - 9380\n— ------’
E + ‘\n\n’

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 ‘\n\n’ == ’ 3 3…---- -----’
E Strings contain only whitespace, escaping them using repr()
E - ’ 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----’
E + ‘\n\n’

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 ‘\n\n’ == ’ 11 38…— ------’
E Strings contain only whitespace, escaping them using repr()
E - ’ 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ — ----- ------’
E + ‘\n\n’

test_module.py:77: AssertionError
____________________ 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 ‘Error: Too many problems’ == ‘Error: Too many problems.’
E - Error: Too many problems.
E ? -
E + Error: Too many problems

test_module.py:77: AssertionError
_____________________ 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 ‘\n\n’ == ‘Error: Numbe… four digits.’
E Strings contain only whitespace, escaping them using repr()
E - ‘Error: Numbers cannot be more than four digits.’
E + ‘\n\n’

test_module.py:77: AssertionError
_______________ 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 ‘\n\n\n’ == ’ 3 9… 858 1028’
E Strings contain only whitespace, escaping them using repr()
E - ’ 3 988\n+ 855 + 40\n----- -----\n 858 1028’
E + ‘\n\n\n’

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 ‘\n\n\n’ == ’ 32 … 172 1028’
E Strings contain only whitespace, escaping them using repr()
E - ’ 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028’
E + ‘\n\n\n’

test_module.py:77: AssertionError
=========================== short test summary info ============================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - Assert…
FAILED test_module.py::test_template[test_two_problems_arrangement2] - Assert…
FAILED test_module.py::test_template[test_four_problems_arrangement] - Assert…
FAILED test_module.py::test_template[test_five_problems_arrangement] - Assert…
FAILED test_module.py::test_template[test_too_many_problems] - AssertionError…
FAILED test_module.py::test_template[test_too_many_digits] - AssertionError: …
FAILED test_module.py::test_template[test_two_problems_with_solutions] - Asse…
FAILED test_module.py::test_template[test_five_problems_with_solutions] - Ass…

I have checked the code many times, change it but I still get errors, what could be the problem?

Take a closer look at the test outputs, each of them has some information, explaining why test failed.

Yes, but it is very complicated, as I just started with scientific computer

You are the most interested in the lines that are starting with E, taking as example:

``````E AssertionError: Expected different output when calling “arithmetic_arranger()” with [“3801 - 2”, “123 + 49”]
E assert ‘\n\n’ == ’ 3801 …---- -----’
E Strings contain only whitespace, escaping them using repr()
E - ’ 3801 123\n- 2 + 49\n------ -----’
E + ‘\n\n’
``````

What does looks like an issue here?

Not sure, but I’m guessing its the “-----------”

Two strings are compared in the test:

``````assert ‘\n\n’ == ’ 3801 …---- -----’
``````

Looking at the line above the output

``````assert actual == expected_output, fail_message
``````

We know that on the left is what is returned by the function and on the right is what is expected. Lower there’s how those two strings would compare line by line. However because string returned by function contain just blank whitespace (new line characters in this case), to make visible anything, those characters are being escaped, so they are displayed:

``````- ’ 3801 123\n- 2 + 49\n------ -----’
+ ‘\n\n’
``````

So, how do I fix such a problem?

Well, the question is - what part of code makes the function return just blank lines?

Im guessing, its the /n

That’s the new line character, yes. Figuring out why function only returns these characters will be a one step closer to fixing the issue.

1 Like

Ok thank you, ill keep at it

So far, I am still unable to figure out the problem, very confusing stuff

Could you link to your replit with the code? It will be easier to help with that.

There aren’t exactly many lines in your code where you use “\n” - the final if-else.
And looking at that, gives me the impression you have no idea what your code does, because they look fine. Meaning that somehow your code results in `first`, `second`, `lines` and `sumx` all being `NONE`.

Please print out values all around in the code and see what’s actually in them.
Because of the large number of test cases, you might want to copy the function into a seperate notebook and test it with one or two test-cases.

Edit:
Dude, you indented almost the ENTIRE CODE wrong. Nothing is getting executed because it’s all within the if-block where you check for non-digits.

So, how should I indent it? Thanks.

Video in this lesson has explained how indentation works in Python:

1 Like

Looking your code in replit, you have the return of the function inside the for loop problem in problems, what mades that only prints the first problem. You should put that part (if solve…) out of the for loop.

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