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: