I’ve managed to get the function to work perfectly when I submit a list via Print().
My problem is when I’m running the actual test_module.py
The problem seems to be with my return statements. Initially, I set the entire thing up so that I return a print statement of the errors. That was an issue as it didn’t return an argument of any kind. Causing the actual tests to see an error from the Print statement and then None a line under.
I’ve changed the first few statements to not behave this way. So I’m printing the error and then returning arranged_problems. The problem with that is that the error is printed and then the list is printed.
What’s the best approach to this? Everything works. But I know there is an issue with the way I’m handling return statements.
Here’s my code:
def arithmetic_arranger(problems, second=False):
arranged_problems = problems
top_row = []
operator = []
bottom_row = []
solutions_row = []
problem_display_length = []
if len(problems) > 5:
print('Error: Too many problems.')
return arranged_problems
arranged_problems = ''
#split to rows
for problem in problems:
items = problem.split()
if len(items[0]) > 4 or len(items[2]) > 4:
print('Error: Numbers cannot be more than four digits.', end='')
return arranged_problems
else:
top_row.append(items[0])
bottom_row.append(items[2])
if items[1] != '+' and items[1] != '-':
print("Error: Operator must be '+' or '-'.", end='')
return arranged_problems
operator.append(items[1])
for item in top_row:
try:
int(item)
except:
return print(" Error: Numbers must only contain digits. ")
for item in bottom_row:
try:
int(item)
except:
return print(" Error: Numbers must only contain digits. ")
#determine length of each problem and run calculations
for x, problem in enumerate(problems):
problem_display_length.append(
len(str(max(int(top_row[x]), int(bottom_row[x])))) + 2
)#which number is longer in each row, then capture length + 2 for operator and space
if operator[x] == '+':
solutions_row.append( str(int(top_row[x]) + int(bottom_row[x])) )
if operator[x] == '-':
solutions_row.append( str(int(top_row[x]) - int(bottom_row[x])) )
#construct top row
for x, problem in enumerate(problems):
arranged_problems += (' ' * ((problem_display_length[x]) - len(str(top_row[x]))) + top_row[x] + ' ')
arranged_problems += '\n'
#construct middle row
#for x, problem in enumerate(problems):
# arranged_problems += (' ' * ((problem_display_length[x]) - len(operator[x])) + operator[x] + ' ')
# arranged_problems += '\n'
#construct bottom row
for x, problem in enumerate(problems):
arranged_problems += (operator[x] + ' ' * ((problem_display_length[x]) - len(bottom_row[x]) - 1) + bottom_row[x] + ' ')
arranged_problems += '\n'
#construct 4 spaces
for x, problem in enumerate(problems):
arranged_problems += ('-' * problem_display_length[x] + ' ')
arranged_problems += '\n'
if second is True:
#construct row of solutions
for x, problem in enumerate(problems):
arranged_problems += (' ' * ((problem_display_length[x]) - len(solutions_row[x])) + solutions_row[x] + ' ')
#construct bottom row
return arranged_problems
Here’s a link to my code: