# Arithmetic Formatter - 6 failures

Hey!
I wrote this code for this project and, in a way, it worked (with some flaws - for example, when I added numbers to the third problem (making the number bigger), there was misalignment).

``````def arithmetic_arranger(problems, sec_arg=False):
if len(problems) > 4:
return print("Error: Too many problems.")

stop = None
for item in problems:
if item.find("*") >= 0:
stop = True
elif item.find("/") >= 0:
stop = True

if stop == True:
return print("Error: Operator must be '+' or '-'.")

str_arr = list() # To store the strings separately.
ope_arr = list() # To store the operators separately.
results = list() # To store the results of each problem.

num_for_while = 0
while num_for_while < len(problems):
temporary = list()
temporary.append(problems[num_for_while])
temporary = temporary[0].split(" ")
temporary_str = [temporary[0], temporary[2]]
temporary_ope = temporary[1]
str_arr.append(temporary_str)
ope_arr.append(temporary_ope)
num_for_while += 1

# To check if operands are digits and if the operands <= 4 digits.
test_arr = list()
while_number = 0
while while_number < len(str_arr):
test_arr += [*str_arr[while_number]]
while_number += 1

for n in test_arr:
try:
int(n)
except:
return print("Error: Numbers must only contain digits.")
if len(n) > 4:
return print("Error: Numbers cannot be more than four digits.")

# To get the results and check if the operators are either + or -.
numb_for_while = 0
while numb_for_while < len(ope_arr):
if ope_arr[numb_for_while] == "+":
results.append(int(str_arr[numb_for_while][0]) + int(str_arr[numb_for_while][1]))
elif ope_arr[numb_for_while] == "-":
results.append(int(str_arr[numb_for_while][0]) - int(str_arr[numb_for_while][1]))
elif ope_arr[numb_for_while] == "*":
print("Error: Operator must be '+' or '-'.")
break
elif ope_arr[numb_for_while] == "/":
print("Error: Operator must be '+' or '-'.")
break
else:
print("Error: Operator must be '+' or '-'.")
break
numb_for_while += 1

len_list = list()
for item in str_arr:
if len(item[0]) > len(item[1]):
len_list.append(len(item[0]))
elif len(item[1]) == len(item[0]):
len_list.append(len(item[0]))
else:
len_list.append(len(item[1]))

dash_list = list()
for item in len_list:
if item == 1:
dash_list.append("---")
elif item == 2:
dash_list.append("----")
elif item == 3:
dash_list.append("-----")
elif item == 4:
dash_list.append("------")

list_of_lists = list()
numb_for_wlist = 0
while numb_for_wlist < len(str_arr):
temporary = list()
temporary.append(str_arr[numb_for_wlist][0])
temporary.append(ope_arr[numb_for_wlist])
temporary.append(str_arr[numb_for_wlist][1])
temporary.append(dash_list[numb_for_wlist])
temporary.append(str(results[numb_for_wlist]))
list_of_lists.append(temporary)
numb_for_wlist += 1

abc = False
if sec_arg == "True" or sec_arg == True:
abc = True
else:
abc = False
if abc == True:

if len(str_arr) == 1:
return print(list_of_lists[0][0].rjust(7),
"\n", list_of_lists[0][1].rjust(0), list_of_lists[0][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6),
"\n", list_of_lists[0][4].rjust(6))
elif len(str_arr) == 2:
return print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9),
"\n", list_of_lists[0][4].rjust(6), list_of_lists[1][4].rjust(9))
elif len(str_arr) == 3:
return print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7),
"\n", list_of_lists[0][4].rjust(6), list_of_lists[1][4].rjust(9), list_of_lists[2][4].rjust(7))
elif len(str_arr) == 4:
return print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7), list_of_lists[3][0].rjust(8),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
list_of_lists[3][1].rjust(4), list_of_lists[3][2].rjust(3),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7), list_of_lists[3][3].rjust(8),
"\n", list_of_lists[0][4].rjust(6), list_of_lists[1][4].rjust(9), list_of_lists[2][4].rjust(7), list_of_lists[3][4].rjust(8))
else:
if len(str_arr) == 1:
return print(list_of_lists[0][0].rjust(7),
"\n", list_of_lists[0][1].rjust(0), list_of_lists[0][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6))
elif len(str_arr) == 2:
return print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9))
elif len(str_arr) == 3:
return print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7))
elif len(str_arr) == 4:
return print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7), list_of_lists[3][0].rjust(8),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
list_of_lists[3][1].rjust(4), list_of_lists[3][2].rjust(3),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7), list_of_lists[3][3].rjust(8))
``````

But when I ran it on the replit, there were 6 failures that I donâ€™t understand.

``````îş§ python main.py
32      3801      45      123
+  698    -    2    + 43    +  49
-----    ------    ----    -----
None
3      3801      45      123
+  855    -    2    + 43    +  49
-----    ------    ----    -----
FError: Operator must be '+' or '-'.
FError: Numbers must only contain digits.
F     32         1      45      123
-  698    - 3801    + 43    +  49
-----    ------    ----    -----
-666     -3800      88      172
FError: Numbers cannot be more than four digits.
FError: Too many problems.
F
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 11, 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"]

======================================================================
FAIL: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 39, in test_incorrect_operator
self.assertEqual(
AssertionError: None != "Error: Operator must be '+' or '-'." : Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."

======================================================================
FAIL: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 57, in test_only_digits
self.assertEqual(
AssertionError: None != 'Error: Numbers must only contain digits.' : Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 66, in test_solutions
self.assertEqual(
AssertionError: None != '   32         1      45      123\n- 698 [90 chars] 172' : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.

======================================================================
FAIL: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 48, in test_too_many_digits
self.assertEqual(
AssertionError: None != 'Error: Numbers cannot be more than four digits.' : Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."

======================================================================
FAIL: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 30, in test_too_many_problems
self.assertEqual(
AssertionError: None != 'Error: Too many problems.' : Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."

----------------------------------------------------------------------
Ran 6 tests in 0.002s

FAILED (failures=6)
``````

I tested some things, like the 4 errors mentioned in readme.md, and the results came out as I expected.

What do I need to fix in the code for it to work?

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36`.

Challenge: Arithmetic Formatter

Function is expected to return string, not print it on it own. `print` function prints the passed in string, but it returns `None`, thatâ€™s why `None` can be seen in the output.

1 Like

Thank you so much!! My bad.

I substitute the â€śreturn print(â€¦)â€ť for â€śprint(â€¦)â€ť and â€śreturn â€¦â€ť

``````def arithmetic_arranger(problems, sec_arg=False):
if len(problems) > 4:
print("Error: Too many problems.")
return "Error: Too many problems."

stop = None
for item in problems:
if item.find("*") >= 0:
stop = True
elif item.find("/") >= 0:
stop = True

if stop == True:
print("Error: Operator must be '+' or '-'.")
return "Error: Operator must be '+' or '-'."

str_arr = list() # To store the strings separately.
ope_arr = list() # To store the operators separately.
results = list() # To store the results of each problem.

num_for_while = 0
while num_for_while < len(problems):
temporary = list()
temporary.append(problems[num_for_while])
temporary = temporary[0].split(" ")
temporary_str = [temporary[0], temporary[2]]
temporary_ope = temporary[1]
str_arr.append(temporary_str)
ope_arr.append(temporary_ope)
num_for_while += 1

# To check if operands are digits and if the operands <= 4 digits.
test_arr = list()
while_number = 0
while while_number < len(str_arr):
test_arr += [*str_arr[while_number]]
while_number += 1

for n in test_arr:
try:
int(n)
except:
print("Error: Numbers must only contain digits.")
return "Error: Numbers must only contain digits."
if len(n) > 4:
print("Error: Numbers cannot be more than four digits.")
return "Error: Numbers cannot be more than four digits."

# To get the results and check if the operators are either + or -.
numb_for_while = 0
while numb_for_while < len(ope_arr):
if ope_arr[numb_for_while] == "+":
results.append(int(str_arr[numb_for_while][0]) + int(str_arr[numb_for_while][1]))
elif ope_arr[numb_for_while] == "-":
results.append(int(str_arr[numb_for_while][0]) - int(str_arr[numb_for_while][1]))
elif ope_arr[numb_for_while] == "*":
print("Error: Operator must be '+' or '-'.")
break
elif ope_arr[numb_for_while] == "/":
print("Error: Operator must be '+' or '-'.")
break
else:
print("Error: Operator must be '+' or '-'.")
break
numb_for_while += 1

len_list = list()
for item in str_arr:
if len(item[0]) > len(item[1]):
len_list.append(len(item[0]))
elif len(item[1]) == len(item[0]):
len_list.append(len(item[0]))
else:
len_list.append(len(item[1]))

dash_list = list()
for item in len_list:
if item == 1:
dash_list.append("---")
elif item == 2:
dash_list.append("----")
elif item == 3:
dash_list.append("-----")
elif item == 4:
dash_list.append("------")

list_of_lists = list()
numb_for_wlist = 0
while numb_for_wlist < len(str_arr):
temporary = list()
temporary.append(str_arr[numb_for_wlist][0])
temporary.append(ope_arr[numb_for_wlist])
temporary.append(str_arr[numb_for_wlist][1])
temporary.append(dash_list[numb_for_wlist])
temporary.append(str(results[numb_for_wlist]))
list_of_lists.append(temporary)
numb_for_wlist += 1

abc = False
if sec_arg == "True" or sec_arg == True:
abc = True
else:
abc = False
if abc == True:

if len(str_arr) == 1:
print(list_of_lists[0][0].rjust(7),
"\n", list_of_lists[0][1].rjust(0), list_of_lists[0][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6),
"\n", list_of_lists[0][4].rjust(6))
return list_of_lists[0][0].rjust(7),
"\n", list_of_lists[0][1].rjust(0), list_of_lists[0][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6),
"\n", list_of_lists[0][4].rjust(6)
elif len(str_arr) == 2:
print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9),
"\n", list_of_lists[0][4].rjust(6), list_of_lists[1][4].rjust(9))
return list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9),
"\n", list_of_lists[0][4].rjust(6), list_of_lists[1][4].rjust(9)
elif len(str_arr) == 3:
print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7),
"\n", list_of_lists[0][4].rjust(6), list_of_lists[1][4].rjust(9), list_of_lists[2][4].rjust(7))
return list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7),
"\n", list_of_lists[0][4].rjust(6), list_of_lists[1][4].rjust(9), list_of_lists[2][4].rjust(7)
elif len(str_arr) == 4:
print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7), list_of_lists[3][0].rjust(8),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
list_of_lists[3][1].rjust(4), list_of_lists[3][2].rjust(3),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7), list_of_lists[3][3].rjust(8),
"\n", list_of_lists[0][4].rjust(6), list_of_lists[1][4].rjust(9), list_of_lists[2][4].rjust(7), list_of_lists[3][4].rjust(8))
return list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7), list_of_lists[3][0].rjust(8),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
list_of_lists[3][1].rjust(4), list_of_lists[3][2].rjust(3),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7), list_of_lists[3][3].rjust(8),
"\n", list_of_lists[0][4].rjust(6), list_of_lists[1][4].rjust(9), list_of_lists[2][4].rjust(7), list_of_lists[3][4].rjust(8)
else:
if len(str_arr) == 1:
print(list_of_lists[0][0].rjust(7),
"\n", list_of_lists[0][1].rjust(0), list_of_lists[0][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6))
return list_of_lists[0][0].rjust(7),
"\n", list_of_lists[0][1].rjust(0), list_of_lists[0][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6)
elif len(str_arr) == 2:
print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9))
return list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9)
elif len(str_arr) == 3:
print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7))
return list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7)
elif len(str_arr) == 4:
print(list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7), list_of_lists[3][0].rjust(8),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
list_of_lists[3][1].rjust(4), list_of_lists[3][2].rjust(3),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7), list_of_lists[3][3].rjust(8))
return list_of_lists[0][0].rjust(7), list_of_lists[1][0].rjust(9), list_of_lists[2][0].rjust(7), list_of_lists[3][0].rjust(8),
"\n", list_of_lists[0][1].rjust(1), list_of_lists[0][2].rjust(4),
list_of_lists[1][1].rjust(4), list_of_lists[1][2].rjust(4),
list_of_lists[2][1].rjust(4), list_of_lists[2][2].rjust(0),
list_of_lists[3][1].rjust(4), list_of_lists[3][2].rjust(3),
"\n", list_of_lists[0][3].rjust(6), list_of_lists[1][3].rjust(9), list_of_lists[2][3].rjust(7), list_of_lists[3][3].rjust(8)
``````

Now it shows only 2 failures.

``````     32      3801      45      123
+  698    -    2    + 43    +  49
-----    ------    ----    -----
('     32', '     3801', '     45', '     123')
3      3801      45      123
+  855    -    2    + 43    +  49
-----    ------    ----    -----
FError: Operator must be '+' or '-'.
.Error: Numbers must only contain digits.
.     32         1      45      123
-  698    - 3801    + 43    +  49
-----    ------    ----    -----
-666     -3800      88      172
FError: Numbers cannot be more than four digits.
.Error: Too many problems.
.
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 11, in test_arrangement
self.assertEqual(
AssertionError: ('      3', '     3801', '     45', '     123') != '    3      3801      45      123\n+ 855 [56 chars]----' : 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/test_module.py", line 66, in test_solutions
self.assertEqual(
AssertionError: ('     32', '        1', '     45', '     123') != '   32         1      45      123\n- 698 [90 chars] 172' : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.004s

FAILED (failures=2)
``````

But I donâ€™t understand these 2 failures. Can anyone help me with them?

Because thereâ€™s multiple elements, separated with comma, python puts them inside of tuple and returns it like that. This worked for `print` function, because it accepts multiple arguments.

1 Like

Thanks!!
Iâ€™m going to review the code and make the adjustments.

I managed to finish this project.
Thanks @sanity!!

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