Arithmetic Formatter errors

When I test the code for the requirements myself it passes all of them.
I don’t understand what the errors mean and why my code fails even thought when I test for conditions myself in pycharm they pass.
However when I put the code in main.py to test it returns errors below:

FError: Operator must be '+' or '-'.
EError: Numbers must only contain digits.
EEError: Numbers cannot be more than four digits.
EError: Too many problems.
E
======================================================================
ERROR: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-8/test_module.py", line 161, in test_incorrect_operator
    actual = arithmetic_arranger(
  File "/home/runner/boilerplate-arithmetic-formatter-8/test_module.py", line 27, in arithmetic_arranger
    exit()
  File "/usr/lib/python3.8/_sitebuiltins.py", line 26, in __call__
    raise SystemExit(code)
SystemExit: None

======================================================================
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-8/test_module.py", line 179, in test_only_digits
    actual = arithmetic_arranger(
  File "/home/runner/boilerplate-arithmetic-formatter-8/test_module.py", line 30, in arithmetic_arranger
    exit()
  File "/usr/lib/python3.8/_sitebuiltins.py", line 26, in __call__
    raise SystemExit(code)
SystemExit: None

======================================================================
ERROR: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-8/test_module.py", line 188, in test_solutions
    actual = arithmetic_arranger(
TypeError: arithmetic_arranger() takes 1 positional argument but 2 were given

======================================================================
ERROR: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-8/test_module.py", line 170, in test_too_many_digits
    actual = arithmetic_arranger(
  File "/home/runner/boilerplate-arithmetic-formatter-8/test_module.py", line 43, in arithmetic_arranger
    exit()
  File "/usr/lib/python3.8/_sitebuiltins.py", line 26, in __call__
    raise SystemExit(code)
SystemExit: None

======================================================================
ERROR: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-8/test_module.py", line 150, in test_too_many_problems
    actual = arithmetic_arranger([
  File "/home/runner/boilerplate-arithmetic-formatter-8/test_module.py", line 22, in arithmetic_arranger
    exit()
  File "/usr/lib/python3.8/_sitebuiltins.py", line 26, in __call__
    raise SystemExit(code)
SystemExit: None

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-8/test_module.py", line 136, in test_arrangement
    self.assertEqual(
AssertionError: None != '    3      3801      45      123\n+ 855 [56 chars]----' : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

----------------------------------------------------------------------
Ran 6 tests in 0.054s

FAILED (failures=1, errors=5)
---------------------------------------------------------------------------------------------------


My code is below

import re
def arithmetic_arranger(problems):

    n = len(problems)
    count = 0
    plus = '+'
    minus = '-'
    multiply = '*'
    divide = '/'
    list_line_1 = []
    list_line_2 = []
    list_line_3 = []
    list_line_4 = []
    space_x = '    '

    if n > 5:
        print("Error: Too many problems.")
        return
    else:
        for count in range(n):
            if multiply in problems[count] or divide in problems[count]:
                print("Error: Operator must be '+' or '-'.")
                return

            if problems[count].islower() or problems[count].isupper():
                print("Error: Numbers must only contain digits.")
                return
            if plus in problems[count]:
                z = re.findall('[0-9]+', problems[count])
                l1 = z[0]
                l2 = z[1]

                len1 = len(l1)
                len2 = len(l2)
                if len1 > 4:
                    print("Error: Numbers cannot be more than four digits.")
                    return
                if len2 > 4:
                    print("Error: Numbers cannot be more than four digits.")
                    return

                plus1 = int(l1)
                plus2 = int(l2)
                l4_int = plus1 + plus2
                l4 = str(l4_int)

                if len1 - len2 >= 1:
                    between_space = len1 - len2
                else:
                    between_space = 0

                btwspace = ' ' * between_space

                if len2 - len1 >= 1:
                    first_rspace = len2 - len1
                else:
                    first_rspace = 0

                frspace = " " * first_rspace
                line1 = frspace + '  ' + l1
                line2 = '+ ' + btwspace + l2
                dashline = '-' * len(line2)
                line3 = dashline
                ans_space = len(line3) - len(l4)
                rans_space = ' ' * ans_space
                line4 = rans_space + l4

                list_line_1.append(line1 + space_x)
                list_line_2.append(line2 + space_x)
                list_line_3.append(line3 + space_x)
                list_line_4.append(line4 + space_x)
                count = count + 1

            elif minus in problems[count]:
                z = re.findall('[0-9]+', problems[count])
                l1 = z[0]
                l2 = z[1]
                len1 = len(l1)
                len2 = len(l2)
                if len1 > 4:
                    print("Error: Numbers cannot be more than four digits.")
                    return
                if len2 > 4:
                    print("Error: Numbers cannot be more than four digits.")
                    return

                min1 = int(l1)
                min2 = int(l2)
                l4_int = min1 - min2
                l4 = str(l4_int)

                if len1 - len2 >= 1:
                    between_space = len1 - len2
                else:
                    between_space = 0

                btwspace = ' ' * between_space

                if len2 - len1 >= 1:
                    first_rspace = len2 - len1
                else:
                    first_rspace = 0

                frspace = " " * first_rspace
                line1 = frspace + '  ' + l1
                line2 = '- ' + btwspace + l2
                dashline = '-' * len(line2)
                line3 = dashline
                ans_space = len(line3) - len(l4)
                rans_space = ' ' * ans_space
                line4 = rans_space + l4

                list_line_1.append(line1 + space_x)
                list_line_2.append(line2 + space_x)
                list_line_3.append(line3 + space_x)
                list_line_4.append(line4 + space_x)


    print(*list_line_1, sep = '')
    print(*list_line_2, sep = '')
    print(*list_line_3, sep = '')
    print(*list_line_4, sep = '')


Challenge: Arithmetic Formatter

Link to the challenge:

Ehm return exits the function without executing the following code. So this should only return a.

Welcome, there.

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 (’).

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

Think about what you intend to return and if the method applied here works.

There should be a file called “arithmetic_arranger.py” or so - > your code should go there.

Also you are not supposed to print any outputs, but return them as a string. Because the unittest determines if that string is identical to the expected output.

Once you fix those two, you might get more meaningful error messages.

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