It seems like all the fails are because of a display problem I’m just not sure what it is.
import re
def arithmetic_arranger(problems, solve = False):
top = ""
bottom = ""
line = ""
answer = ""
#Errors
for problem in problems:
if(re.search("[*]" or "[/]" , problem)):
return "Error: equations must be addition or subtraction."
first_num = problem.split(" ")[0]
operator = problem.split(" ")[1]
second_num = problem.split(" ")[2]
if(len(first_num) >= 5) or len(second_num) >= 5:
return "Error: numbers cannot be more than four digits."
if(re.search("[^sa-zA-Z0-9.+-]" , first_num or second_num)):
return "Error: Numbers must only contain digits."
sum = ""
if(operator == "+"):
sum = str(int(first_num) + int(second_num))
elif(operator == "-"):
sum = str(int(first_num) - int(second_num))
length = max(len(first_num), len(second_num))
top = str(first_num).rjust(length)
bottom = operator + str(second_num)
line = ""
answer = str(sum).rjust(length)
for x in range(length):
line +="-"
if problem != problems[-1]:
top += top + " "
bottom += bottom + " "
line += line + " "
answer += answer + " "
else :
top += top
bottom += bottom
line += line
answer += answer
if solve:
display = top + "/n" + bottom + "/n" + line + "/n" + answer
else:
display = top + "/n" + bottom + "/n" + line
print(display)
One of the errors:
_______________ test_template[test_five_problems_with_solutions] _______________
arguments = [['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True]
expected_output = ' 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.'
@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 solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E assert None == (' 32 1 45 123 988\n'\n '- 698 - 3801 + 43 + 49 + 40\n'\n '----- ------ ---- ----- -----\n'\n ' -666 -3800 88 172 1028')
E +None
E -' 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028'
test_module.py:77: AssertionError