ive been having trouble fixing this since I have no idea why this happens. Basically when I use the four spaces to separate the problems, it goes all crazy. When I used the exact same code on pycharm (and adding a print function just before the return statement, both having the same argument/result) it works perfectly. I tried to use ‘\t’ instead of the spaces and even though it looks better, the problems still dont align perfectly
def arithmetic_arranger(problems, answers=False):
#too many problems solution
if len(problems) > 5:
return 'Error: Too many problems.'
#variables for the output/return results
first = ''
second = ''
dash = ''
answer = ''
#Main for loop to get each problem seperately
for problem in problems:
#for loop to solve the correct operators error
for characters in problem:
characters.replace(' ', '')
if '/' in characters or '*' in characters:
return "Error: Operator must be '+' or '-'."
#variables to get each individual item of the problem
problemx = problem.split()
problemx1 = problemx[0]
problemxo = problemx[1]
problemx2 = problemx[2]
#solution to the max digits Error
if len(problemx1) >= 5 or len(problemx2) >= 5:
return 'Error: Numbers cannot be more than four digits.'
#if statement for the answer variable and for the only digits Error solution
if problemxo == '+':
try:
add = int(problemx1) + int(problemx2)
answer += str(add).rjust(len(max(problem)) + 2) + " "
except:
return 'Error: Numbers must only contain digits.'
elif problemxo == '-':
try:
sub = int(problemx1) - int(problemx2)
answer += str(sub).rjust(len(max(problem)) + 2) + " "
except:
return 'Error: Numbers must only contain digits.'
#lenght variable for correct formatting.
if len(problemx1) > len(problemx2):
lenght = len(problemx1)
elif len(problemx2) > len(problemx1):
lenght = len(problemx2)
elif len(problemx2) == len(problemx1):
lenght = len(problemx1)
#building up the variables for the output
first += problemx1.rjust(lenght + 2) + ' '
second += problemxo + problemx2.rjust(lenght + 1) + ' '
dash += '-' * (lenght + 2) + ' '
#if statement to output the results with answers or no answers
if answers:
return first + "\n" + second + "\n" + dash + "\n" + answer
else:
return first + "\n" + second + "\n" + dash + "\n"
And this is the error
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-5/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: ' [23 chars] 123 \n+ 855 - 2 + 43 + 49 [37 chars] \n' != ' [23 chars] 123\n+ 855 - 2 + 43 + 49\n-----[23 chars]----'
- 3 3801 45 123
? ----
+ 3 3801 45 123
- + 855 - 2 + 43 + 49
? ----
+ + 855 - 2 + 43 + 49
- ----- ------ ---- -----
? -----
+ ----- ------ ---- ----- : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]