Hi all,

I cannot for the life of me figure out why this solution doesn’t pass the automated tests. When I run this in the terminal, the math problems come out formatted exactly like shown in the assignment description.

(I realize this isn’t the most concise code and needs to be refactored/DRY’d out, but for now I just want to know why the functionality isn’t working as expected).

Thanks in advance.

```
import re
def arithmetic_arranger(problems, show_solution=False):
problem_dictionaries = []
first_line = ""
second_line = ""
third_line = ""
fourth_line = ""
#Error handling (all working as expected):
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
if any(char.isalpha() for char in problem):
return "Error: Numbers must only contain digits."
elif any(char in ["*", "/"] for char in problem):
return "Error: Operator must be '+' or '-'."
else:
nums = re.findall(r"\d+", problem)
for num in nums:
if len(num) > 4:
return "Error: Numbers cannot be more than four digits."
#Creating a dictionary out of each math problem
for problem in problems:
parsed_problem = {}
nums = re.findall(r"\d+", problem)
operator = re.findall(r"[+-]", problem)[0]
if operator == "+":
solution = int(nums[0]) + int(nums[1])
if operator == "-":
solution = int(nums[0]) - int(nums[1])
problem_width = max(len(str(abs(solution))), len(str(nums[0])),
len(str(nums[1]))) + 2
if problem_width < 3:
problem_width = 3
elif problem_width > 6:
problem_width = 6
parsed_problem['first_num'] = nums[0]
parsed_problem['operator'] = operator[0]
parsed_problem['second_num'] = nums[1]
parsed_problem['width'] = problem_width
parsed_problem['solution'] = str(solution)
problem_dictionaries.append(parsed_problem)
#Formatting each dictionary into lines
if show_solution:
for problem_dict in problem_dictionaries:
for _ in range(
1,
problem_dict['width'] - (len(problem_dict['first_num']))):
first_line += " "
first_line += problem_dict['first_num']
first_line += " "
second_line += problem_dict['operator']
for _ in range(
1,
problem_dict['width'] - (len(problem_dict['second_num']))):
second_line += " "
second_line += problem_dict['second_num']
second_line += " "
for _ in range(0, problem_dict['width']):
third_line += "_"
third_line += " "
for _ in range(
0, problem_dict['width'] - len(problem_dict['solution'])):
fourth_line += " "
fourth_line += problem_dict['solution']
fourth_line += " "
arranged_problems = f"{first_line}\n{second_line}\n{third_line}\n{fourth_line}"
else:
for problem_dict in problem_dictionaries:
for _ in range(
1,
problem_dict['width'] - (len(problem_dict['first_num']))):
first_line += " "
first_line += problem_dict['first_num']
first_line += " "
second_line += problem_dict['operator']
for _ in range(
1,
problem_dict['width'] - (len(problem_dict['second_num']))):
second_line += " "
second_line += problem_dict['second_num']
second_line += " "
for _ in range(0, problem_dict['width']):
third_line += "_"
third_line += " "
arranged_problems = f"{first_line}\n{second_line}\n{third_line}"
return arranged_problems
```