Scientific Computing with Python Projects - Arithmetic Formatter

Hi! Me again.
I got errors. I do not understand. The issue:

================================================== test session starts ===================================================
platform linux -- Python 3.10.8, 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] PASSED                                             [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] PASSED                                            [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-    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
_____________________________________ 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           + 2    - 9380
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           + 855    -    2    + 43    +  49
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           +  4    - 2999    + 2    +  49    - 9380
E         - ----    ------    ---    -----    ------
E         + ----    ------    ---    -----    ------
E         ?                                         +
E         +   15       802      3      172     -9379

test_module.py:77: AssertionError
================================================ short test summary info =================================================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - AssertionError: Expected different output when c...
FAILED test_module.py::test_template[test_two_problems_arrangement2] - AssertionError: Expected different output when c...
FAILED test_module.py::test_template[test_four_problems_arrangement] - AssertionError: Expected different output when c...
FAILED test_module.py::test_template[test_five_problems_arrangement] - AssertionError: Expected different output when c...
============================================== 4 failed, 6 passed in 0.19s ===============================================

My code:

def arithmetic_arranger(problems, show = True):
  #print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))
  arranged_problems = []
  #too many problems supplied
  if len(problems)>5:
    return "Error: Too many problems."
  for i, ele in enumerate(problems):
    sub = ele.split()
    num1 = sub[0]
    oper = sub[1]
    num2 = sub[2]
    #appropriate operators "+","-"
    if sub[1] not  in "-+":
      return "Error: Operator must be '+' or '-'."
    #only contain digits
    if sub[0].isdigit() and sub[2].isdigit() is False:
        return "Error: Numbers must only contain digits."
    #max 4 digits
    if len(sub[0])>4 or len(sub[2])>4:
      return "Error: Numbers cannot be more than four digits."
      
    ray = "-"

    maxlen_line = max(len(num1), len(num2))
    ach = maxlen_line + 2

    line_num1 = f"{num1:>{ach}}"
    line_num2 = oper + f"{num2:>{ach-1}}"
    line_rays = ray*ach
    
    try:
      arranged_problems[0] += (" "*4) + line_num1
    except IndexError:
      arranged_problems.append(line_num1)
    try:
      arranged_problems[1] += (" "*4) + line_num2
    except IndexError:
      arranged_problems.append(line_num2)
    try:
      arranged_problems[2] += (" "*4) + line_rays
    except IndexError:
      arranged_problems.append(line_rays)
    answ = 0 
    if oper == "+":
      answ = int(num1) + int(num2)
    else:
      answ = int(num1) - int(num2)
    
    line_answ = f"{str(answ):>{ach}}"
    try:
      arranged_problems[3] += (" "*4) + line_answ
    except IndexError:
      arranged_problems.append(line_answ)
  if show == True:
    arranged_problems = f"{arranged_problems[0]}\n{arranged_problems[1]}\n{arranged_problems[2]}\n{arranged_problems[3]}"
  else:
    arranged_problems = f"{arranged_problems[0]}\n{arranged_problems[1]}\n{arranged_problems[2]}"
      
    
  return arranged_problems

Thaks for all :slight_smile:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.78

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

You have an extra character (invisible character) to the immediate right side of the dash line.

1 Like

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