Arithmetic Formatter (Python) Problem

Tell us what’s happening:

Hello everyone,

I am having a problem passing this certification project, everything seems to be working as intended when I tested it in Sublime Text, but when I tested it on the repl.it link it gives an error message (attached at the end of this post). Is there something that I did wrong?

I noticed some of the code is outside the grey box, how should I properly post a code?

Also, feel free to leave a suggestion on how the code should be improved.

Your code so far

    def arithmetic_arranger(problems, boole = False):
	import operator
	ops = {"+": operator.add, "-": operator.sub}

	sproblems, errcount, arranged_problems = ([], 0, '')
	
	#error conditions and the creation of answers and dashes	
	for problem in problems:
		sproblem = problem.split()
		sproblems.append(sproblem)
		if sproblem[0].isdigit() == False or sproblem[2].isdigit() == False:
			arranged_problems = 'Error: Numbers must only contain digits.'
			errcount += 1
		if len(sproblem[0]) > 4 or len(sproblem[2]) > 4:
			arranged_problems = 'Error: Numbers cannot be more than four digits.'
			errcount += 1 
		if sproblem[1] != '+' and sproblem[1] != '-':
			arranged_problems = "Error: Operator must be '+' or '-'."
			errcount += 1
		if len(problems) > 5:
			arranged_problems = 'Error: Too many problems.'
			errcount += 1
		if errcount > 0:
			return arranged_problems
			quit()
		sproblem.append('-'*(max(len(sproblem[0]), len(sproblem[2])) + 2))
		if boole == True:
			sproblem.append(str(ops[sproblem[1]](int(sproblem[0]), int(sproblem[2]))))

	#print(sproblems)

	ap1, ap2, ap3, ap4 = ('', '', '', '')
	
	#list elements formation into a string
	for prob in sproblems:
		ap1 += '  ' + ' '*((max(len(prob[0]), len(prob[2])) + 2) - len(prob[0])) + prob[0] + '  '
		if len(prob[0]) >= len(prob[2]): 
			ap2 += '  '+ prob[1] + ' '*(len(prob[0]) - len(prob[2]) + 1) + prob[2] + '  '
		else:
			ap2 += '  '+ prob[1] + ' ' + prob[2] + '  '
		ap3 += prob[3] + '    '
		try: ap4 += '  ' + ' '*((max(len(prob[0]), len(prob[2])) + 2) - len(prob[4])) + prob[4] + '  '
		except: continue

	ap1, ap2, ap3, ap4 = (ap1[2:(len(ap1)-2)], ap2[2:(len(ap2)-2)], ap3[:(len(ap3)-4)], ap4[2:(len(ap4)-2)])
	if ap4 == '':
		arranged_problems += ap1 + '\n' + ap2 + '\n' + ap3 + '\n'
	else:
		arranged_problems += ap1 + '\n' + ap2 + '\n' + ap3 + '\n' + ap4

	return arranged_problems

#print(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"], True))

#Thank you for your time.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36.

Challenge: Arithmetic Formatter

Link to the challenge:

Error message in repl.it:

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

F.....
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/BackRegularComputers/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: '    [36 chars]   -    2    + 43    +  49\n-----    ------    ----    -----\n' != '    [36 chars]   -    2    + 43    +  49\n-----    ------    ----    -----'
      3      3801      45      123
  + 855    -    2    + 43    +  49
- -----    ------    ----    -----
?                                 -
+ -----    ------    ----    ----- : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

----------------------------------------------------------------------
Ran 6 tests in 0.051s

FAILED (failures=1)

you may have an extra space at the end, that’s what the dash seems to be indicating


I don’t think I do, I made sure to clear out the extra spaces by slicing the string right after the 2nd loop

EDIT: you’re right, I forgot to delete the extra new line on the final assignments of the arranged_problems. Thank you for pointing that out, it got no errors now.

Here’s the fix:

def arithmetic_arranger(problems, boole = False):
import operator
ops = {"+": operator.add, “-”: operator.sub}

sproblems, errcount, arranged_problems = ([], 0, '')

#error conditions and the creation of answers and dashes	
for problem in problems:
	sproblem = problem.split()
	sproblems.append(sproblem)
	if sproblem[0].isdigit() == False or sproblem[2].isdigit() == False:
		arranged_problems = 'Error: Numbers must only contain digits.'
		errcount += 1
	if len(sproblem[0]) > 4 or len(sproblem[2]) > 4:
		arranged_problems = 'Error: Numbers cannot be more than four digits.'
		errcount += 1 
	if sproblem[1] != '+' and sproblem[1] != '-':
		arranged_problems = "Error: Operator must be '+' or '-'."
		errcount += 1
	if len(problems) > 5:
		arranged_problems = 'Error: Too many problems.'
		errcount += 1
	if errcount > 0:
		return arranged_problems
		quit()
	sproblem.append('-'*(max(len(sproblem[0]), len(sproblem[2])) + 2))
	if boole == True:
		sproblem.append(str(ops[sproblem[1]](int(sproblem[0]), int(sproblem[2]))))

#print(sproblems)

ap1, ap2, ap3, ap4 = ('', '', '', '')

#list elements formation into a string
for prob in sproblems:
	ap1 += '  ' + ' '*((max(len(prob[0]), len(prob[2])) + 2) - len(prob[0])) + prob[0] + '  '
	if len(prob[0]) >= len(prob[2]): 
		ap2 += '  '+ prob[1] + ' '*(len(prob[0]) - len(prob[2]) + 1) + prob[2] + '  '
	else:
		ap2 += '  '+ prob[1] + ' ' + prob[2] + '  '
	ap3 += prob[3] + '    '
	try: ap4 += '  ' + ' '*((max(len(prob[0]), len(prob[2])) + 2) - len(prob[4])) + prob[4] + '  '
	except: continue

ap1, ap2, ap3, ap4 = (ap1[2:(len(ap1)-2)], ap2[2:(len(ap2)-2)], ap3[:(len(ap3)-4)], ap4[2:(len(ap4)-2)])
if ap4 == '':
	arranged_problems += ap1 + '\n' + ap2 + '\n' + ap3
else:
	arranged_problems += ap1 + '\n' + ap2 + '\n' + ap3 + '\n' + ap4

return arranged_problems

#print(arithmetic_arranger([“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”], True))