Arithmetic-formatter

Hello Everyone,

I am stuck with the project for 2 days as I cannot pass the test cases, my output is identical. Here’s my code,

def arithmetic_arranger(problems, solve=False):

  if (len(problems) > 5):
    return "Error: Too many problems."

  first = ""
  second = ""
  lines = ""
  sumf = ""
  string = ""

  for p in problems:
    firstNum = p.split(" ")[0]
    operator = p.split(" ")[1]
    secondNum = p.split(" ")[2]

    if (not firstNum.isnumeric() or not secondNum.isnumeric()):
      return "Error: Numbers must only contain digits."

    if len(firstNum) > 4 or len(secondNum) > 4:
      return "Error: Numbers cannot be more than four digits."

    if (operator == "/" or operator == "*"):
      return "Error: Operator must be '+' or '-'."

    sum = ""

    if operator == "+":
      sum = str(int(firstNum) + int(secondNum))

    elif operator == "-":
      sum = str(int(firstNum) - int(secondNum))

    length = max(len(firstNum), len(secondNum)) + 2

    top = str(firstNum).rjust(length)
    bottom = str(operator) + str(secondNum).rjust(length - 1)
    line = ""
    ans = str(sum).rjust(length)

    for l in range(length):
      line += "-"

    if p != p[-1]:
      first += top + '    '
      second += bottom + '    '
      lines += line + '    '
      sumf += ans + '    '

    else:
      first += top
      second += bottom
      lines += line
      sumf += ans

  if solve:
    string = first + "\n" + second + "\n" + lines + "\n" + sumf
  else:
    string = first + "\n" + second + "\n" + lines

  return string
1 Like

Please post the errors you are getting.

1 Like
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]
1 Like

this error is saying that you have an extra set of 4 spaces after each line that are unexpected.

2 Likes

hi Nathan, this is a certification project. You have to be careful how much help you give especially when no specific question has been asked. (otherwise it appears to be bordering on cheating)

Thank you, Yes my formatting conditional statement was the one with the issue as I needed to do p != problems[-1] instead of p != p[-1]. Thank you again for the help.

2 Likes

how do i import my project to replit please am new to it…
i have already created an account on replit.
but i dont know how else to go about it. :face_exhaling: :palms_up_together:

1 Like

i failed to import my project to replit
now its a month down when am trying to figure out and this is my first project.

please help me

1 Like

What do you mean by “import my project”? Normally people just click the replit reject link from the fCC page and it opens.

1 Like