Arithmetic Formatter : 2 Failures not getting clear errors

Good wishes everybody.

Below is the error message i am getting when i execute my code here. I am getting proper output while the code is executed in the VS Code. Will be grateful for your help. After the error message i have pasted the code of arithemtic_arranger function.

Error message :


    32      3801      45      123    
 + 698    -    2    + 43    +  49    
 -----    ------    ----    -----    
 
F..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: '     3      3801      45      123    \n + 855[69 chars] \n ' != '    3      3801      45      123\n+ 855    - [51 chars]----'
-      3      3801      45      123    
? -                                ----
+     3      3801      45      123
-  + 855    -    2    + 43    +  49    
? -                                ----
+ + 855    -    2    + 43    +  49
-  -----    ------    ----    -----    
? -                                -----
+ -----    ------    ----    ------   : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/test_module.py", line 39, in test_solutions
    self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.')
AssertionError: '    32         1      45      123    \n - 69[106 chars]    ' != '   32         1      45      123\n- 698    -[86 chars] 172'
-     32         1      45      123    
? -                                ----
+    32         1      45      123
-  - 698    - 3801    + 43    +  49    
? -                                ----
+ - 698    - 3801    + 43    +  49
-  -----    ------    ----    -----    
? -                                ----
+ -----    ------    ----    -----
-   -666     -3800      88      172    ? -                                ----
+  -666     -3800      88      172 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.018s

Arithmetic Arranger function code :

def arithmetic_arranger(problems, to_display = False):

    # validating list 

    # 1. list must have maximum 5 sums.

    # 2. Operators must '+' or '-'.

    # 3. Numbers must only contains digit. 

    # 4. Number cannot be more than four digits. 

    Operator1 = 0 # for saving first operator

    Operator2 = 0 # for saving second operator

    Operand = ''

    # 1. list must have maximum 5 sums.

    if len(problems) > 5:

        # print('Too many problems.')

        return 'Error: Too many problems.'

    for sum in problems :

        # seperating operators and operands from the sum

        sum = sum.split()

        Operand = sum[1]

    # 2. Operators must '+' or '-'.

        if Operand != '+' and Operand != '-':

            # print(Operand)

            # print("Error: Operator must be '+' or '-'.")

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

    # 3. Numbers must only contains digits.

        try:

            Operator1 = int(sum[0])

            Operator2 = int(sum[2])

        except ValueError:

            # print("Error: Numbers must only contain digits.")

            return "Error: Numbers must only contain digits."

    # 4. Number cannot be more than four digits.

        if len(sum[0]) > 4 or len(sum[2]) > 4:

            # print("Error: Numbers cannot be more than four digits.")

            return "Error: Numbers cannot be more than four digits."

    # Calculating results and displaying. 

    op1_list = []

    op2_list = []

    op3_list = []

    for sum in problems:

        sum = sum.split()

        op1_list.append(sum[0] )

        op2_list.append(sum[1])

        op3_list.append(sum[2])

    # Printing first operand horizontally 

    counter = 0

    first_line = ''

    while counter < len(op1_list) :

        # print(op1_list[counter].rjust((len(op3_list[counter]) + 2) if len(op3_list[counter]) > len(op1_list[counter]) else len(op1_list[counter]) + 2) , end = " " * 4)

        first_line = first_line + str(op1_list[counter].rjust((len(op3_list[counter]) + 2) if len(op3_list[counter]) > len(op1_list[counter]) else len(op1_list[counter]) + 2)) + " " * 4

        counter = counter + 1

    # print("Printing first line")

    # print(first_line)

    # print(' ')

    # Printing operator and second operand horizontally.

    counter = 0

    second_line = ''

    while counter < len(op2_list) :

        # print(op2_list[counter] , end = " ")

        # print(op3_list[counter].rjust((len(op3_list[counter])) if len(op3_list[counter]) > len(op1_list[counter]) else len(op1_list[counter])) , end = " " * 4)

        second_line = second_line + str(op2_list[counter]) + " " + str(op3_list[counter].rjust((len(op3_list[counter])) if len(op3_list[counter]) > len(op1_list[counter]) else len(op1_list[counter]))) + " " * 4

        counter = counter + 1

    # print(" ")

    # print("printing both lines ")

    # print(first_line + '\n'+ second_line)

    # Printing "--" between result and operand / operators.

    counter = 0

    third_line = ''

    while counter < len(op3_list) :

        # print('-' * (len(op3_list[counter]) + 2 if len(op3_list[counter]) > len(op1_list[counter]) else len(op1_list[counter]) + 2) , end = " " * 4)

        third_line = third_line + '-' * (len(op3_list[counter]) + 2 if len(op3_list[counter]) > len(op1_list[counter]) else len(op1_list[counter]) + 2)  + " " * 4

        counter = counter + 1

    # print("")

    # print("printing three lines ")

    # print(first_line + '\n'+ second_line + '\n' + third_line)

    # Printing results 

    counter = 0

    fourth_line = " "

    while counter < len(op3_list) :

        result = 0

        if to_display == True:

            if op2_list[counter] == '-' :

                result = int(op1_list[counter]) - int(op3_list[counter])

            elif op2_list[counter] == '+':

                result = int(op1_list[counter]) + int(op3_list[counter])

            # print(str(result).rjust((len(op3_list[counter]) + 2) if len(op3_list[counter]) > len(op1_list[counter]) else len(op1_list[counter]) + 2) , end = " " * 4 )

            fourth_line = fourth_line + str(result).rjust((len(op3_list[counter]) + 2) if len(op3_list[counter]) > len(op1_list[counter]) else len(op1_list[counter]) + 2) + " " * 4

        counter = counter + 1

    return " " + first_line + '\n'+ " "+ second_line +  '\n' +" "+ third_line +  '\n' + fourth_line

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

you have a diff (differential) show of the result:
the lines with - are your code, the lines with + are what it should be and the lines with ? are showing the points of difference

ieahleen.

Thank you very much for updating the content. Will surely take care of it next time.

However I am attaching screenshots of the requirement against the result i am getting while executing the code. I believe the changes is only for some space but not able figure it out. I wish if you can review and help.

Again grateful for your comments.

Respect.

Krutarth

the diff says you have extra characters at beginning and end of each line

example, 4 spaces at the end of line

extra character at the beginning

Yes anticipated the same and tried with both options.
Changed to 3 instead of 4 in all lines and also removed the first space as well.
Got the same error messages.
However will try some other way around.
Thank you so much for prompt reply and kind help.
Regards
Krutarth

hi ieahleen.

Thank you for your help and it is solved now. It was trailing space that was not shown on the screen but with strip function this was solved.

Again thank you for showing your concern and wish you good time ahead.

Krutarth

you can just remove the code in which you add them, less wasteful, in that line I quoted you have written + " " * 4 at the end of the line

Yes I followed that but it was removing the spaces from each sum.
The problem was due to that even after the last sum there were 4 spaces and were now removed with rstrip function.

Again thank you for showing me the direction.