Tell us what’s happening:
so i think getting error because of formatting of the answer
Your code so far
import re
def arithmetic_arranger(problems, *args):
if len(problems) > 5:
return "Error: Too many problems."
num1_arr =[]
num2_arr =[]
dashes_arr =[]
ans_arr = []
for equation in problems:
equation = equation.split(" ")
num1 = equation[0]
sign = equation[1]
num2 = equation[2]
regex = "\D"
reg_sign = re.search("[+-]", sign)
if reg_sign:
if re.search(regex, num1) or re.search(regex, num2):
return "Error: Numbers must only contain digits."
if len(num1) > 4 or len(num2) > 4:
return "Error: Numbers cannot be more than four digits."
biggest_num = max(len(num1), len(num2))
spaces = biggest_num + 2
dashes = "-"*(spaces)
answer = eval(f"{num1} {sign} {num2}")
num1_arr.append(f"{int(num1):>{spaces}}")
num2_arr.append(f"{sign}{int(num2):>{spaces-1}}")
dashes_arr.append(dashes)
ans_arr.append(f"{answer:>{spaces}}")
else:
return "Error: Operator must be '+' or '-'."
arranged_problems = [" ".join(num1_arr).rstrip()," ".join(num2_arr).rstrip()," ".join(dashes_arr).rstrip()," ".join(ans_arr).rstrip()]
arranged_problems2 = "\n".join(arranged_problems)
#print( arranged_problems2)
return arranged_problems2
the output
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/fcc-arithmetic-arranger/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: ' [72 chars] ------ ---- -----\n 858 3799 88 172' != ' [72 chars] ------ ---- -----'
3 3801 45 123
+ 855 - 2 + 43 + 49
- ----- ------ ---- -----
? -
11 3801 1 123 1+ 4 - 2999 + 2 + 49 - 9380---- ------ --- ----- ------
15 802 3 172 -9379
F.....===============================================================
=======
FAIL: test_arrangement (test_module.UnitTests)
---------------------------------------------------------------
-------
Traceback (most recent call last):
File "/home/runner/fcc-arithmetic-arranger/test_module.py", l
ine 10, in test_arrangement
self.assertEqual(actual, expected, 'Expected different outp
ut when calling "arithmetic_arranger()" with ["3 + 855", "3801
- 2", "45 + 43", "123 + 49"]')
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_arra
nger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
---------------------------------------------------------------
-------
Ran 6 tests in 0.003s
FAILED (failures=1)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0
.
Challenge: Arithmetic Formatter
Link to the challenge: