A little help with Python Arithmetic Formatter?

Hi all, I am new here and would like some help with the first project of Scientific Computing with Python. It is driving me nuts especially since I don’t understand the error feedback this time.
My project link: https://repl.it/join/pymkyytk-ryoukei

My code:

def arithmetic_arranger(problems, showAns = False):
    if len(problems) > 5:
        return "Error: Too many problems."
    else:
        topline = list()
        midline = list()
        botline = list()
        final = list()
        for problem in problems:
            problem = problem.split()
            n1 = problem[0]
            operator = problem[1]
            n2 = problem[2]
            if operator not in ["+", "-"]:
                return "Error: Operator must be '+' or '-'."
            elif n1.isdigit() is False or n2.isdigit() is False:
                return "Error: Numbers must only contain digits."
            elif len(n1) > 4 or len(n2) > 4:
                return "Error: Numbers cannot be more than four digits."
            else:
                if operator == "+":
                    answer = int(n1) + int(n2)
                else:
                    answer = int(n1) - int(n2)
                width = max(len(n1), len(n2)) + 2
                top = str(n1.rjust(width))
                mid = str(operator.ljust(width - max(len(n1), len(n2)) - 1) + n2.rjust(max(len(n1), len(n2)) + 1))
                bottom = str("-" * width)
                last = str(answer).rjust(width)
                topline.append(top)
                midline.append(mid)
                botline.append(bottom)
                final.append(last)

    x = "    ".join(topline)
    y = "    ".join(midline)
    z = "    ".join(botline)
    a = "    ".join(final)

    if showAns == True:
        return x + "\n" + y + "\n" + z + "\n" + a
    else:
        return x + "\n" + y + "\n" + z + "\n" + a

The error message I get is:

+ 855    -    2    + 43    +  49
-----    ------    ----    -----
  858      3799      88      172
F.....
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/test_module.py", line 10, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: '    [68 chars]-    ------    ----    -----\n  858      3799      88      172' != '    [68 chars]-    ------    ----    -----'
      3      3801      45      123
  + 855    -    2    + 43    +  49
- -----    ------    ----    -----
?                                 -
+ -----    ------    ----    ------   858      3799      88      172 : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

----------------------------------------------------------------------
Ran 6 tests in 0.003s

FAILED (failures=1)

Any help is appreciated! Thanks in advance.

Notice that example called from the main.py and printed when project is run, doesn’t have set argument to print out answers, but answers are still printed.

I am not sure I follow. The code returns the value to the function caller which already has print() around it?

arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]) call from main.py returns the following:

    3      3801      45      123
+ 855    -    2    + 43    +  49
-----    ------    ----    -----
  858      3799      88      172

Remember that writing out answers is optional in function and depends on passing appropriate argument when function is called. That function call doesn’t have set argument to add answers to problems, but they are returned anyway.

Welp, there goes my comprehension. I am sorry, are you saying that I should change the function 'arithmetic_arranger’s parameters? I am completely lost.

Take a look at the examples in the README.md file, first one doesn’t add answers (results of the calculations) for the problems, while the second does - as it has the argument to do that set.

Omg, I see what you mean. I did not see that part of the requirement. Thanks for the guidance!

1 Like

Sorry to bother you again, but do you know how to accept your comment as the solution on this forum. I can’t seem to find how to lock this thread as solved.

I think button for that should be somewhere at the bottom of the post, in the section where there’s also Reply button to reply to specific post.