# Stuck on Arithmetic Formatter

I am stuck on the Arithmetic Formatter project. I have written the following code but cannot understand the source of failure.

CODE:

``````def arithmetic_arranger(problems, showAns = False):

if len(problems)>5:
return "Error: Too many problems."
else:
arranged_problems = []
for i in problems:
x = i.split()
n1 = x
n2 = x
operand = x

if operand not in ["+", "-"]:
return "Error: Operator must be '+' or '-'."
else:
for digit in n1:
if digit not in ["0","1","2","3","4","5","6","7","8","9"]:
return "Error:  Numbers must only contain digits."
else:
continue

for digit in n2:
if digit not in ["0","1","2","3","4","5","6","7","8","9"]:
return "Error: Numbers must only contain digits."
else:
continue

if len(n1)>4 or len(n2)>4:
return "Error: Numbers cannot be more than four digits."
else:

if operand == '+':
else:

width = max(len(n1),len(n2)) +2
topLine = str(n1.rjust(width))
secondLine =  str(operand + " " + n2)
dashLine = str("-" * width)

if showAns == True:
arranged_problems.append(topLine + "\n" + secondLine + "\n" + dashLine + "\n" + answerLine)
else:
arranged_problems.append(topLine + "\n" + secondLine + "\n" + dashLine)

return arranged_problems
``````

Error Msg:

``````['   32\n+ 698\n-----', '  3801\n- 2\n------', '  45\n+ 43\n----', '  123\n+ 49\n-----']
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/fcc-arithmetic-arranger/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\n+ 855\n-----', '  3801\n- 2\n---[42 chars]---'] != '    3      3801      45      123\n+ 855 [56 chars]----' : 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/fcc-arithmetic-arranger/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 arithemetic problems and a second argument of `True`.')
AssertionError: ['   32\n- 698\n-----\n -666', '     1\n-[73 chars]172'] != '   32         1      45      123\n- 698 [90 chars] 172' : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.001s

FAILED (failures=2)
``````

Thanks!

I can’t test it now, but what about passing simple arrays to test how your function is working?

Maybe add some `console.log`’ too.

I’m not too sure the first splitting is correct , so young can check that out.

can you give the link to your repl? so it is easier to debug

This is the result from simply printing arithmatic_arranger with the test values:

``````['   32\n+ 698\n-----', '  3801\n- 2\n------', '  45\n+ 43\n----', '  123\n+ 49\n-----']
``````

it appears split seems to be working correctly

you are returning an array of strings, you need to return a single string

Omg thank you! that solved it!

# this is my code

def calculate_operands_operation(operation, op1,op2):
if operation == ‘+’ :
return op1+op2
elif operation == ‘-’:
return op1 - op2
elif operation == '’:
return op1
op2
else :
if op2 == 0 :
raise('Division By 0 ')
else : return op1/op2

def arithmetic(expression = ‘’, mode=‘Ver’):
# list of the arithmetic operations
operations = [’+’, ‘-’, ‘/’, ‘*’]

``````# Variable True if expression valid / False: else
valid_expression = False

# Checking if expression = '' if True Function return None
if expression =='':
return None
# Expression in not empty
else :
# Split the expression using space as separator
my_expression = expression.split()

# Checking the validation of the entered expression
for i in range(len(my_expression)-1) :
if my_expression[i].isdigit() :
# in arithmetic operation the operand will followed by operation
# else return invalid expression
if my_expression[i+1] in operations :
valid_expression = True
else :
valid_expression = False
break
else :
# if the expression start with operation in this case the operation
# will be followed by un operand
# else : invalid expression
if my_expression[i+1].isdigit() :

valid_expression = True
else :
valid_expression = False
break

if valid_expression:

if mode == 'Ver' :
result = calculate_operands_operation(
my_expression, float(my_expression),float(my_expression)
)
print('    ', float(my_expression))
print(my_expression)
print('    ', float(my_expression))
print('-------')
print('    ',result)
for i in range(3,len(my_expression),2) :

oper = my_expression[i]
op1= my_expression[i+1]
print(oper)
print('    ',float(my_expression[i+1]))
result = calculate_operands_operation(
operation=oper,
op1=float(result),
op2=float(op1)

)
print('-------')
print('    ',result)

return result
elif mode == 'Hor' :
result = calculate_operands_operation(
my_expression, float(my_expression), float(my_expression)
)
print(float(my_expression),end =' ')
print(my_expression, end = ' ')
print(float(my_expression), end=' = ')

print(result, end = ' ')
for i in range(3, len(my_expression), 2):
oper = my_expression[i]
op1 = my_expression[i + 1]
print(oper, end=' ')
print(float(my_expression[i + 1]), end= ' = ')
result = calculate_operands_operation(operation=oper,op1=float(result),op2=float(op1))

print(result, end =' ')

return result

else :
return None #Invalid expression
``````

if name == ‘main’:
result = arithmetic(expression=input(‘Enter the expression that will be evaluated :’),
mode= input(‘Select the mode [Ver/Hor] : ‘))
print(’\n’)
print('The result is : ===> ', result)