Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
I can’t find the issue in my code but it keeps showing the same mistake

Your code so far

def arithmetic_arranger(problems, show_answers=False):
  if len(problems) > 5:
    return "Error: Too many problems."

  arranged_problems = []
  for problem in problems:
    if '+' not in problem and '-' not in problem:
      return "Error: Operator must be '+' or '-'."
    operand1, operator, operand2 = problem.split()
    if not operand1.isdigit() or not operand2.isdigit():
      return "Error: Numbers must only contain digits."
    if len(operand1) > 4 or len(operand2) > 4:
      return "Error: Numbers cannot be more than four digits."

    operand1 = operand1.rjust(max(len(operand1), len(operand2)))
    operand2 = operand2.rjust(max(len(operand1), len(operand2)))
    operator = operator.rjust(max(len(operand1), len(operand2)))
    dashes = '-' * max(len(operand1), len(operand2))

    arranged_problem = f"{operand1}    {operand2}\n{operator}    {dashes}"
    if show_answers:
      result = str(eval(problem))
      result = result.rjust(max(len(operand1), len(operand2)))
      arranged_problem += f"\n{result}"
    arranged_problems.append(arranged_problem)

  return '    '.join(arranged_problems)

Your browser information:

User Agent is: < @fcc8bf0e45a-25ee-4a22-8766-091faabb73c2>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

Can you share the error message(s) here?

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

yes sure here is the error message:
python3 main.py
32 698

  • — 3801 2
  • ---- 45 43
  • – 123 49

============================= test session starts ==============================
platform linux – Python 3.8.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 – /home/runner/boilerplate-arithmetic-formatter/venv/bin/python3
cachedir: .pytest_cache
rootdir: /home/runner/boilerplate-arithmetic-formatter
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 2\n - ---- 123 49\n + —’ == ’ 3801 123\n- 2 + 49\n------ -----’
E - 3801 123
E ? – ^ -
E + 3801 2
E ? ^
E - - 2 + 49
E - ------ -----
E + - ---- 123 49
E + + —

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 2\n+ - 1 9380\n - ----’ == ’ 1 1\n+ 2 - 9380\n— ------’
E - 1 1
E - + 2 - 9380
E - — ------
E + 1 2
E + + - 1 9380
E + - ----

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 855\n’\n ’ + — 3801 2\n’\n ’ - ---- 45 43\n’\n ’ + – 123 49\n’\n ’ + —‘) == (’ 3 3801 45 123\n’\n ‘+ 855 - 2 + 43 + 49\n’\n ‘----- ------ ---- -----’)
E - 3 3801 45 123
E - + 855 - 2 + 43 + 49
E - ----- ------ ---- -----
E + 3 855
E + + — 3801 2
E + - ---- 45 43
E + + – 123 49
E + + —

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 4\n’\n ’ + – 3801 2999\n’\n ’ - ---- 1 2\n’\n ‘+ - 123 49\n’\n ’ + — 1 9380\n’\n ’ - ----‘) == (’ 11 3801 1 123 1\n’\n ‘+ 4 - 2999 + 2 + 49 - 9380\n’\n ‘---- ------ — ----- ------’)
E - 11 3801 1 123 1
E - + 4 - 2999 + 2 + 49 - 9380
E - ---- ------ — ----- ------
E + 11 4
E + + – 3801 2999
E + - ---- 1 2
E + + - 123 49
E + + — 1 9380
E + - ----

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 855\n + —\n858 988 40\n + —\n1028’ == ’ 3 988\n+ 855 + 40\n----- -----\n 858 1028’
E - 3 988
E - + 855 + 40
E - ----- -----
E - 858 1028
E + 3 855
E + + —
E + 858 988 40
E + + —
E + 1028

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 698\n’\n ’ - —\n’\n ‘-666 1 3801\n’\n ’ - ----\n’\n ‘-3800 45 43\n’\n ’ + –\n’\n ‘88 123 49\n’\n ’ + —\n’\n ‘172 988 40\n’\n ’ + —\n’\n ‘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 - - 698 - 3801 + 43 + 49 + 40
E - ----- ------ ---- ----- -----
E - -666 -3800 88 172 1028
E + 32 698
E + - —
E + -666 1 3801
E + - ----
E + -3800 45 43
E + + –
E + 88 123 49
E + + —
E + 172 988 40
E + + —
E + 1028

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_two_problems_with_solutions] - Asse…
FAILED test_module.py::test_template[test_five_problems_with_solutions] - Ass…
========================= 6 failed, 4 passed in 0.28s ==========================

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