Help! arithmetic formatter

Tell us what’s happening:
The code is already working but it keeps printing this:

Console

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
  730      3799      88      172

F.....
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\Documents\Personal\Education\Programming\Python\ArithmeticFormatter\test_module.py", line 10, in test_arrangement
    self.assertEqual(actual, expected,
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.014s

FAILED (failures=1)

This is my code so far.
Your code so far

def arithmetic_arranger(problems, op_par=False):
    first = ""
    second = ""
    answer = ""
    line = "-"
    lines = ''

    # ERROR #
    # TOO MANY PROBLEMS (LIMIT IS 5)
    if len(problems) >= 6:
        return 'Error: Too many problems.'

    for i in problems:
        operation = i.split()
        first_number = operation[0]
        operator_digit = operation[1]
        second_number = operation[2]

        # ERROR #
        # NUMBER HAS MORE THAN 4 DIGITS
        if len(first_number) > 4 or len(second_number) > 4:
            return "Error: Numbers cannot be more than four digits."

        # ERROR #
        # NUMBER CONTAINS A LETTER CHARACTER
        if not first_number.isnumeric() or not second_number.isnumeric():
            return "Error: Numbers must only contain digits."

        # ERROR #
        # OPERATOR IS '/' OR '*'
        if operator_digit == '+' or operator_digit == '-':
            if operator_digit == '+':
                result = str(int(first_number) + int(second_number))
            else:
                result = str(int(first_number) - int(second_number))

            length = max(len(first_number), len(second_number)) + 2
            top_line = first_number.rjust(length)
            bottom_line = operator_digit + second_number.rjust(length - 1)
            result_line = result.rjust(length)

            if i != problems[-1]:
                first += top_line + '    '
                second += bottom_line + '    '
                lines += line*length + '    '
                answer += result_line + '    '

            else:
                first += top_line
                second += bottom_line
                lines += line * length
                answer += result_line

        else:
            return "Error: Operator must be '+' or '-'."

    first.rstrip()
    second.rstrip()
    lines.rstrip()

    if op_par:
        arranged_problems = first + "\n" + second + "\n" + lines + "\n" + answer

    else:
        arranged_problems = first + "\n" + second + "\n" + lines

    return arranged_problems

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0.

Challenge: Arithmetic Formatter

Link to the challenge:

You should use return "Error: Too many problems."
due to exit statements, your program is giving error


remember you’ve to add line*length


I think you’ll figure out what’s wrong with this


Let me know if this doesn’t work

Well yes and no hahah, it worked well for not returning the whole list of errors, just like you said. It was using return instead of print(), exit().

I didn’t understand the part of:
lines += line + ’ ’
lines += line

Can you further explain that please? :slight_smile:

Thank you, your reply did help a lot!

Your line="-" and below every problem you have to add -(hyphens) as per the length of numbers…
Hence you should write lines += line*length

You’re printing this out for debugging, but forgot to add it to the returning string

When “op_par=True” it prints the previous thing.
When “op_par=False” it prints the following:

......
----------------------------------------------------------------------
Ran 6 tests in 0.000s

OK
   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

Process finished with exit code 0

I don’t know what am I doing wrong. :frowning:

Are you talking about these :arrow_up: previous thing …

Did you forgot that if op_par=True it will print the answer below dashed line?