Tell us what’s happening:
Describe your issue in detail here.
Hello everyone!
I completed my first project, the arithmetic formatter for the scientific computing with Python course. As far as I’m concerned, the results I’m getting follow all of the rules specified by the assignment. However assignment is not passing all of the tests. I have even counted spaces manually. I don’t know how to proceed. Any help would be appreciated!
Link to replit code: boilerplate-arithmetic-formatter-1 - Replit
Error I get:
python main.py
32 3801 45 123
- 698 - 2 + 43 + 49
730 3799 88 172
========================== test session starts ===========================
platform linux – Python 3.8.13, pytest-7.1.1, pluggy-1.0.0 – /nix/store/06c55y0c5yzx5gx4l6k0pp6071zf1y5i-python3-3.8.13/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] PASSED [ 50%]
test_module.py::test_template[test_incorrect_operator] PASSED [ 60%]
test_module.py::test_template[test_too_many_digits] PASSED [ 70%]
test_module.py::test_template[test_only_digits] PASSED [ 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 (’ 3801 123 \n’\n ‘- 2 + 49 \n’\n ‘------ ----- \n’\n ’ 3799 172 ‘) == ’ 3801 123\n- 2 + 49\n------ -----’
E - 3801 123
E + 3801 123
E ? ++++
E - - 2 + 49
E + - 2 + 49
E ? ++++
E - ------ -----
E + ------ -----
E ? +++++
E + 3799 172
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 ’ 1 1 \n+ 2 - 9380 \n— ------ \n 3 -9379 ’ == ’ 1 1\n+ 2 - 9380\n— ------’
E - 1 1
E + 1 1
E ? ++++
E - + 2 - 9380
E + + 2 - 9380
E ? ++++
E - — ------
E + — ------
E ? +++++
E + 3 -9379
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 (’ 3 3801 45 123 \n’\n ‘+ 855 - 2 + 43 + 49 \n’\n ‘----- ------ ---- ----- \n’\n ’ 858 3799 88 172 ‘) == (’ 3 3801 45 123\n’\n ‘+ 855 - 2 + 43 + 49\n’\n ‘----- ------ ---- -----’)
E - 3 3801 45 123
E + 3 3801 45 123
E ? ++++
E - + 855 - 2 + 43 + 49
E + + 855 - 2 + 43 + 49
E ? ++++
E - ----- ------ ---- -----
E + ----- ------ ---- -----
E ? +++++
E + 858 3799 88 172
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 (’ 11 3801 1 123 1 \n’\n ‘+ 4 - 2999 + 2 + 49 - 9380 \n’\n ‘---- ------ — ----- ------ \n’\n ’ 15 802 3 172 -9379 ‘) == (’ 11 3801 1 123 1\n’\n ‘+ 4 - 2999 + 2 + 49 - 9380\n’\n ‘---- ------ — ----- ------’)
E - 11 3801 1 123 1
E + 11 3801 1 123 1
E ? ++++
E - + 4 - 2999 + 2 + 49 - 9380
E + + 4 - 2999 + 2 + 49 - 9380
E ? ++++
E - ---- ------ — ----- ------
E + ---- ------ — ----- ------
E ? +++++
E + 15 802 3 172 -9379
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 ’ 3 988 \n+ 855 + 40 \n----- ----- \n 858 1028 ’ == ’ 3 988\n+ 855 + 40\n----- -----\n 858 1028’
E - 3 988
E + 3 988
E ? ++++
E - + 855 + 40
E + + 855 + 40
E ? ++++
E - ----- -----
E + ----- -----
E ? ++++
E - 858 1028
E + 858 1028
E ? ++++
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 1 45 123 988 \n’\n ‘- 698 - 3801 + 43 + 49 + 40 \n’\n ‘----- ------ ---- ----- ----- \n’\n ’ -666 -3800 88 172 1028 ‘) == (’ 32 1 45 123 988\n’\n ‘- 698 - 3801 + 43 + 49 + 40\n’\n ‘----- ------ ---- ----- -----\n’\n ’ -666 -3800 88 172 1028’)
E - 32 1 45 123 988
E + 32 1 45 123 988
E ? ++++
E - - 698 - 3801 + 43 + 49 + 40
E + - 698 - 3801 + 43 + 49 + 40
E ? ++++
E - ----- ------ ---- ----- -----
E + ----- ------ ---- ----- -----
E ? ++++
E - -666 -3800 88 172 1028
E + -666 -3800 88 172 1028
E ? ++++
test_module.py:77: AssertionError
======================== short test summary info =========================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - …
FAILED test_module.py::test_template[test_two_problems_arrangement2] - …
FAILED test_module.py::test_template[test_four_problems_arrangement] - …
FAILED test_module.py::test_template[test_five_problems_arrangement] - …
FAILED test_module.py::test_template[test_two_problems_with_solutions]
FAILED test_module.py::test_template[test_five_problems_with_solutions]
====================== 6 failed, 4 passed in 0.30s =======================
Your code so far
def arithmetic_arranger(problems, to_add = True):
if len(problems) > 5:
return ‘Error: Too many problems.’
def final_string():
line_1_final = ‘’
line_2_final = ‘’
line_3_final = ‘’
line_4_final = ‘’
for i in problems:
if '+' in i:
is_add = True
op = '+'
op_position = i.find('+')
formatted = format_print(i, op, op_position)
if formatted == "Error: Operator must be '+' or '-'." or formatted == 'Error: Numbers must only contain digits.':
return formatted
elif formatted == 'Error: Numbers cannot be more than four digits.':
return formatted
lines_contents = format_print(i, op, op_position)
line_1_final += lines_contents[0] + ' '
line_2_final += lines_contents[1] + ' '
line_3_final += lines_contents[2] + ' '
line_4_final += lines_contents[3] + ' '
elif '-' in i:
is_sub = True
op = '-'
op_position = i.find('-')
lines_contents = format_print(i, op, op_position)
line_1_final += lines_contents[0] + ' '
line_2_final += lines_contents[1] + ' '
line_3_final += lines_contents[2] + ' '
line_4_final += lines_contents[3] + ' '
else:
return "Error: Operator must be '+' or '-'."
last = f'{line_1_final}\n{line_2_final}\n{line_3_final}'
if to_add == True:
last = f'{line_1_final}\n{line_2_final}\n{line_3_final}\n{line_4_final}'
return last
def format_print(string,op,op_position):
n1 = string[0:op_position].strip()
n2 = string[op_position + 1:].strip()
try:
int(n1)
int(n2)
except:
return ‘Error: Numbers must only contain digits.’
if len(n1) > 4 or len(n2) > 4:
return ‘Error: Numbers cannot be more than four digits.’
if len(n1) > len(n2):
num_dashes = len(n1) + 2
else:
num_dashes = len(n2) + 2
n1_right_aligned = n1.rjust(num_dashes)
n2_right_aligned = n2.rjust(num_dashes-1)
op_left_aligned = op.ljust(num_dashes)
line_2 = op + n2_right_aligned
dashes = ''
if op == '+':
result = int(n1) + int(n2)
result_string = str(result)
elif op == '-':
result = int(n1) - int(n2)
for n in range(num_dashes):
dashes += '-'
result_string = str(result)
result_aligned = result_string.rjust(num_dashes)
final = [n1_right_aligned, line_2, dashes, result_aligned]
return final
return final_string()
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: