My code is working and displaying errors on the test data supplied when i am passing it into the function, but the Run test cases are failing for the same.
Not sure why the tests are failing. when i am calling the function with the given test data as in the test cases , i am getting the error returned as you can see in the screenshot
Tweaked the function and now i am able to return the errors rather printing the errors within the function. But now currently stuck at returning the correct formatted problems, Although my output is generating the desired result but the tests are not passing.
def arithmetic_arranger(problems, show_answers=False):
line1 = []
line2 = []
line3 = []
line4 = []
er=0
if len(problems)>5:
return('Error: Too many problems.')
for k in problems:
if '+' in k:
parts = k.split('+')
n = [item.strip() for item in parts]
if n[0].isdigit() and n[1].isdigit():
pass
else:
return("Error: Numbers must only contain digits.")
er=1
break
operator = '+'
elif '-' in k:
parts = k.split('-')
n = [item.strip() for item in parts]
if n[0].isdigit() and n[1].isdigit():
pass
else:
return("Error: Numbers must only contain digits.")
er=1
break
operator = '-'
else:
er=1
return("Error: Operator must be '+' or '-'.")
break
left = parts[0].strip()
right = parts[1].strip()
width = max(len(left), len(right)) + 2
if len(left)>4 or len(right)>4:
return('Error: Numbers cannot be more than four digits.')
er=1
break
line1.append(left.rjust(width))
line2.append(operator + right.rjust(width - 1))
line3.append('-' * width)
if show_answers:
result = str(eval(k))
line4.append(result.rjust(width))
if er==0:
a='\t'.join(line1)
b='\t'.join(line2)
c='\t'.join(line3)
if show_answers:
e= '\t'.join(line4)
d=a+'\n'+b+'\n'+c+'\n'+e
return(d)
else:
d=a+'\n'+b+'\n'+c
return(d)
#print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')
#print(f'\n{arithmetic_arranger(["3 + 855", "988 + 40"],True)}')
arithmetic_arranger(["3801 - 2", "123 + 49"],True)
to see what the first test function call returns you get: ' 3801\t 123\n- 2\t+ 49\n------\t-----', which has quite a few differences from the desired output