# Arithmetic Arranger Failing Tests

Hey everyone,

I have been stuck trying to pass the Arithmetic Arranger project for the past little while. I am failing 2 tests out of the 6 and I cannot seem to figure out what is missing in my code. I copied the code into IDLE and the formatting looked fine for all the tests I did myself but there must be something that I am not seeing. Would someone be able to take a look at my code and give me some guidance in where I went wrong?

``````def arithmetic_arranger(problems, printOutput=False):
formattedStrings = ['', '', '', '']
if len(problems) < 5:
for i in range(len(problems)):
components = problems[i].split(' ')
if components == '+' or components == '-':
if components.isnumeric(
) == True and components.isnumeric() == True:
if len(components) < 5 and len(components) < 5:
#Create the formatted question strings
for i in range(4):
if (formattedStrings[i] == ''):
formattedStrings[i] += ''
else:
formattedStrings[i] += '    '

#In this part of the code, the equations are formatted with a right align and added to a list (to be printed later)

#If second number is shorter (or numbers both same length)
if (len(components) >= len(components)):
formattedStrings += components + ' ' * (
len(components) - len(components) + 1)
formattedStrings += components
formattedStrings += ' ' * 2 + components

#If the first number is shorter
else:
formattedStrings += ' ' * (
len(components) - len(components) + 2
)  # Extra 2 spaces are to compensate for operator and space between digits
formattedStrings += components
formattedStrings[
1] += components + ' ' + components

#In this part of the code, the solution is created and added (if specified) and the other lines are finished

maxLength = max(len(components), len(components))
formattedStrings += '-' * (maxLength + 2)

if (printOutput == True):
sol = ''
if (components == '+'):
sol = str(
int(components) + int(components))
else:
sol = str(
int(components) - int(components))

formattedStrings += ' ' * (
maxLength - len(sol) + 2)
formattedStrings += sol

else:
return (
'Error: Numbers cannot be more than four digits.')

else:
return ('Error: Numbers must only contain digits.')
else:
return ("Error: Operator must be '+' or '-'.")

arrangedProblems = ''

for i in range(len(formattedStrings)):
if (i != len(formattedStrings) - 1):
arrangedProblems += formattedStrings[i] + '\n'
else:
arrangedProblems += formattedStrings[i]

return arrangedProblems
else:
return ("Error: Too many problems.")

``````

Thanks

Michael

make sure the top row has 5 spaces starting from the left. i think you are over a space that’s why it’s not aligning with the bottom spacing. have a good day!

I tried playing around with the spacing a bit but the test still remained. Would you be able to give me an example of one of the tests that had messed up spacing? I have been having troubles understanding the output of the test code and seeing an example would help me diagnose where there is a flaw in my logic. Looking at the test outputs seems there’s issue when, in top line number is just a single digit. In such case function for some reason outputs one space not enough before that number and the further numbers on the right get misaligned as well later. You can copy lines below to the `main.py` file to see it in the full outputs form the function.
``````print(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))