Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Every time I run the code on replit it tells me I have errors in the console. I can gather that it probably has something to do with the spacing given the shows an assert statement where the two strings are not equivalent. I’m just not sure how to read the output from pytest. How can I tell the difference between what the expected output is and the output my program is giving?

Error Message from Pytest

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-----    ----' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         ? -          -
E         +  3801     123
E         - -    2    +  49
E         ?     -      -
E         + -   2    + 49
E         - ------    -----
E         ?      -        -
E         + -----    ----

test_module.py:77: AssertionError

My Code

def arithmetic_arranger(problems, show_answers=False):
    arranged_problems = ''
    invalid_operators = ['*', '/', r'//']
    problem_limit = 5
    digit_limit = 4
    top_lines = []
    bottom_lines = []
    under_lines = []
    answers = []
    
    # check if we are over the problem limit
    if len(problems) > problem_limit:
      return "Error: Too many problems."
    # split the problems up and check formatting errors
    for problem in problems:
      terms = problem.split()
      if len(terms[0]) > digit_limit or len(terms[2]) > digit_limit:
        return "Error: Numbers cannot be more than four digits."
      if terms[1] in invalid_operators:
        return "Error: Operator must be '+' or '-'."
      # check if both operands are only digits
      try:
        term_1 = int(terms[0])
        term_2 = int(terms[2])
      except ValueError:
        return "Error: Numbers must only contain digits."
      if terms[1] == '+':
        answer = term_1 + term_2
      else:
        answer = term_1 - term_2
    # properly formatting string
      line_length = max(len(terms[0]), len(terms[2])) + 1
      top_line = terms[0].rjust(line_length)
      if len(terms[2]) == max(len(terms[0]), len(terms[2])):
        bottom_line = (terms[1] + " ") +  (terms[2].rjust(line_length -1))

      else:
            bottom_line = (terms[1]) +  (terms[2].rjust(line_length -1))
      solution = str(answer).rjust(line_length)
      under_line = "-" * line_length
      
    #  put each line into an array for mass formatting
      top_lines.append(top_line)
      bottom_lines.append(bottom_line)
      under_lines.append(under_line)
      answers.append(solution)

      final_top_line = ("    ").join(top_lines)
      final_bottom_line = ("    ").join(bottom_lines)
      final_under_lines = ("    ").join(under_lines)
      final_answers = ("    ").join(answers)

    if show_answers:
      arranged_problems = ("\n").join([final_top_line, final_bottom_line, final_under_lines, final_answers])
    else:
      arranged_problems = ("\n").join([final_top_line, final_bottom_line, final_under_lines])
      
      
      


    return arranged_problems


Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

-   3801      123
? -          -
+  3801     123
- -    2    +  49
?     -      -
+ -   2    + 49
- ------    -----
?      -        -
+ -----    ----

The test is giving you a “diff”. It looks like you are including too many spaces for each problem.

1 Like

So we are clear for my future knowledge the + line is the expected output?
I solved the problem by removing the space on

Thank you for your help!

1 Like

It depends upon how diff is called.

Is there a standard way to call diff people will use when doing tests?

1 Like

Not really. You can diff(expected, found) or diff(found, expected)

1 Like

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