I am getting an error and I can’t understand what is going on
I am doing the Arithmetic Formatter challenge and I can’t understand what is the problem please help anyone
My code so far
My code
import re
def arithmetic_arranger(problems,solve=False):
if(len(problems)>5):
return "Error: Too many problems."
top=""
bot=""
line=""
sumt=""
arranged_problems=""
for problem in problems:
stat=problem.split(' ')
if(re.search("[^\s0-9,+-]",problem)):
if(re.search("[/]",problem) or re.search("[*]",problem)):
return "Error: Operator must be '+' or '-'."
return "Error: Numbers must only contain digits."
num1=stat[0]
oper=stat[1]
num2=stat[2]
if(len(num1)>=5 or len(num2)>=5):
return "Error: Numbers cannot be more than four digits."
if(oper=='+'):
sum=str(int(num1)+int(num2))
elif(oper=='-'):
sum=str(int(num1)-int(num2))
le=0
le=max(len(num1),len(num2))+2
if(problem!=problems[-1]):
top+=str(num1).rjust(le)+' '
bot+=oper+str(num2).rjust(le-1)+' '
for i in range(le):
line+="-"
line+=' '
if(solve):
sumt+=str(sum).rjust(le)+' '
else:
top+=str(num1).rjust(le)
bot+=oper+str(num2).rjust(le-1)
for i in range(le):
line+="-"
if(solve):
sumt+=str(sum).rjust(le)
arranged_problems+=top+'\n'+bot+'\n'+line+'\n'+sumt
return arranged_problems
My output
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
============================= test session starts ==============================
platform linux -- Python 3.8.12, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /home/runner/boilerplate-arithmetic-formatter
collected 10 items
test_module.py FFFF...... [100%]
=================================== FAILURES ===================================
________________ test_template[test_two_problems_arrangement1] _________________
arguments = [['3801 - 2', '123 + 49']]
expected_output = ' 3801 123\n- 2 + 49\n------ -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E assert ' 3801 ...-- -----\n' == ' 3801 ...---- -----'
E Skipping 36 identical leading characters in diff, use -v to show
E - -- -----
E + -- -----
E ? +
test_module.py:77: AssertionError
________________ test_template[test_two_problems_arrangement2] _________________
arguments = [['1 + 2', '1 - 9380']]
expected_output = ' 1 1\n+ 2 - 9380\n--- ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]
E assert ' 1 ...- ------\n' == ' 1 ...--- ------'
E 1 1
E + 2 - 9380
E - --- ------
E + --- ------
E ? +
test_module.py:77: AssertionError
________________ test_template[test_four_problems_arrangement] _________________
arguments = [['3 + 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = ' 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
E assert ' 3 3...-- -----\n' == ' 3 3...---- -----'
E Skipping 87 identical leading characters in diff, use -v to show
E - -- -----
E + -- -----
E ? +
test_module.py:77: AssertionError
________________ test_template[test_five_problems_arrangement] _________________
arguments = [['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380']]
expected_output = ' 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
> assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]
E assert ' 11 38...- ------\n' == ' 11 38...--- ------'
E Skipping 111 identical leading characters in diff, use -v to show
E - - ------
E + - ------
E ? +
test_module.py:77: AssertionError
=========================== short test summary info ============================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - Assert...
FAILED test_module.py::test_template[test_two_problems_arrangement2] - Assert...
FAILED test_module.py::test_template[test_four_problems_arrangement] - Assert...
FAILED test_module.py::test_template[test_five_problems_arrangement] - Assert...
========================= 4 failed, 6 passed in 0.17s ==========================
Challenge: Arithmetic Formatter
Link to the challenge:
