Hi - I’m getting the correct answer in Visual Studio and repl.it shows I’m correct, but then throws two errors:
Code:
def arithmetic_arranger(problems, *display):
arranged_problems = []
if display:
display = display[0]
top_row_indents = []
first_operands = []
spaces = []
second_row_operators = []
second_row_spans = []
second_operands = []
dashes = []
bottom_row_indents = []
answers = []
num_problems = len(problems)
if num_problems > 5:
return "Error: Too many problems."
for problem in problems:
three_part_question = problem.split(" ")
first_operand = three_part_question[0]
second_operand = three_part_question[2]
first_operands.append(first_operand)
if len(first_operand) > 4 or len(second_operand) > 4:
return "Error: Numbers cannot be more than four digits."
if not first_operand.isdigit() or not second_operand.isdigit():
return "Error: Numbers must only contain digits."
operator = three_part_question[1]
if operator == "*" or operator == "/":
return "Error: Operator must be '+' or '-'."
#determine number of dashes and second row spaces for each problem
if len(first_operand) > len(second_operand):
num_dashes = len(first_operand) + 2
second_row_spaces = (len(first_operand) - len(second_operand)) + 1
else:
num_dashes = len(second_operand) + 2
second_row_spaces = 1
dashes.append(num_dashes)
second_row_spans.append(second_row_spaces)
#four spaces between problems
four_spaces = 4
spaces.append(four_spaces)
#top row formatting
top_row_indent = num_dashes - len(first_operand)
top_row_indents.append(top_row_indent)
#second row formatting
second_row_operator = three_part_question[1]
second_row_operators.append(second_row_operator)
second_operands.append(second_operand)
#answer line formatting
if three_part_question[1] == "+":
answer = int(three_part_question[0]) + int(three_part_question[2])
elif three_part_question[1] == "-":
answer = int(three_part_question[0]) - int(three_part_question[2])
bottom_line_indent = num_dashes - len(str(answer))
bottom_row_indents.append(bottom_line_indent)
answers.append(answer)
if display == True:
for each_problem in range(num_problems):
print(str(" " * top_row_indents[each_problem]) + str(
first_operands[each_problem]) + str(" " * spaces[each_problem]), end='')
arranged_problems.append(str(" " * top_row_indents[each_problem]) + str(
first_operands[each_problem]) + str(" " * spaces[each_problem]))
print('')
for each_problem in range(num_problems):
print(str(second_row_operators[each_problem]) + str(" " * second_row_spans[each_problem]) +
str(second_operands[each_problem]) + str(" " * spaces[each_problem]), end='')
print('')
for each_problem in range(num_problems):
print(str("-" * dashes[each_problem]) +
str(" " * spaces[each_problem]), end='')
print('')
for each_problem in range(num_problems):
print(str(" " * bottom_row_indents[each_problem]) + str(answers[each_problem]) + str(" " * spaces[each_problem]), end = '')
for each_problem in range(num_problems):
line1 = str(str(" " * top_row_indents[each_problem]) + str(
first_operands[each_problem]) + str(" " * spaces[each_problem]))
line2 = str(str(second_row_operators[each_problem]) + str(" " * second_row_spans[each_problem])
+ str(second_operands[each_problem]) + str(" " * spaces[each_problem]))
line3 = str((str("-" * dashes[each_problem]) +
str(" " * spaces[each_problem])))
line4 = str((str(" " * bottom_row_indents[each_problem]) + str(
answers[each_problem]) + str(" " * spaces[each_problem])))
return line1 + line2 + line3 + line4
arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43",
"123 + 49"], True)
Errors:
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/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: ' 3 + 855 ----- 858 ' != ' 3 3801 45 123\n+ 855 - 2 [46 chars]----'
- 3 + 855 ----- 858
+ 3 3801 45 123
+ 855 - 2 + 43 + 49
----- ------ ---- -----
: 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-1/test_module.py", line 39, in test_solutions
self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: ' 32 - 698 ----- -666 ' != ' 32 1 45 123\n- 698 - 3801 [80 chars] 172'
- 32 - 698 ----- -666
+ 32 1 45 123
- 698 - 3801 + 43 + 49
----- ------ ---- -----
-666 -3800 88 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.005s
FAILED (failures=2)
-666 -3800 88 172