Arithmetic_arranger, can't tell why I'm not passing

I’ve finished my code, and when I run individual tests, they all output correctly, but the test module only has me passing 6/10. Can someone spot what the issue is? Code is below:

def arithmetic_arranger(problems, solutions=False):
	#check that problems isn't too long
	if len(problems) > 5:
		return 'Error: Too many problems.'

	final = []
	arranged_problems = '' 

	#split earch string into its 2 numbers and its operand
	for i, v in enumerate(problems):
		final.append(v.split())
		#if operand is anything other than + or -, return error
		if (final[i][1] != '+' and final[i][1] != '-'):
			return 'Error: Operator must be \'+\' or \'-\'.'
		#if any number is greater than 4 digits, return error
		if (len(final[i][0]) > 4 or len(final[i][2]) > 4):
			return 'Error: Numbers cannot be more than four digits.'
		#if any number contains characters other than numbers, return error
		if (final[i][0].isnumeric() == False or final[i][2].isnumeric() == False):
			return 'Error: Numbers must only contain digits.'

		#for each math problem there's a width, equal to 2 + len(longest number)	
		if (len(final[i][0]) > len(final[i][2])):
			width = 2 + len(final[i][0])
		else:
			width = 2 + len(final[i][2])
		
		#insert spaces before the top number, len(v) == 4
		final[i].insert(0, ' ' * (width - len(final[i][0])))
		#insert spaces after the top number, except for the last problem, len(v) == 5
		if i != len(problems) - 1:
			final[i].insert(2, '    ')
		else:
			final[i].insert(2, '\n')
		#insert spaces between operand and bottom number, len(v) == 6
		final[i].insert(4, ' ' * (width - 1 - len(final[i][4])))
		#insert spaces after the bottom number, except for the last problem, len(v) == 7
		if i != len(problems) - 1:
			final[i].append('    ')
		else:
			final[i].append('\n')
		#insert dashes, len(v) == 8
		final[i].append('-' * width)
		#insert space after dashes, len(v) == 9
		if i != len(problems) - 1:
			final[i].append('    ')
		else:
			final[i].append('\n')
		#insert optional solutions
		if solutions == True:			
			#calculate solution and turn it into a string, len(v) == 10
			if final[i][4] == '+':
				final[i].append(str(int(final[i][1]) + int(final[i][5])))
			else:
				final[i].append(str(int(final[i][1]) - int(final[i][5])))
			#insert space before solution, len(v) == 11
			final[i].insert(9, ' ' * (width - len(final[i][9])))
			#insert space after solution, len(v) == 12
			if i != len(problems) - 1:
				final[i].append('    ')
			else:
				final[i].append('')

	#populate line1
	for i in final:
		arranged_problems = arranged_problems + i[0] + i[1] + i[2]
	#populate line2
	for i in final:
		arranged_problems = arranged_problems + i[3] + i[4] + i[5] + i[6]
	#populate line3
	for i in final:
		arranged_problems = arranged_problems + i[7] + i[8]
	#populate optional line4
	if solutions == True:
		for i in final:
			arranged_problems = arranged_problems + i[9] + i[10] + i[11]

	return arranged_problems

What errors are you getting?

I’m up to 8 passed, the two failing are the n_problems_with_solutions tests. The error is:

AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" ...

here’s my updated code; I think the tests don’t like some whitespace somewhere?

def arithmetic_arranger(problems, solutions=False):
	#check that problems isn't too long
	if len(problems) > 5:
		return 'Error: Too many problems.'

	final = []
	arranged_problems = '' 

	#split earch string into its 2 numbers and its operand
	for i, v in enumerate(problems):
		final.append(v.split())
		#if operand is anything other than + or -, return error
		if (final[i][1] != '+' and final[i][1] != '-'):
			return 'Error: Operator must be \'+\' or \'-\'.'
		#if any number is greater than 4 digits, return error
		if (len(final[i][0]) > 4 or len(final[i][2]) > 4):
			return 'Error: Numbers cannot be more than four digits.'
		#if any number contains characters other than numbers, return error
		if (final[i][0].isnumeric() == False or final[i][2].isnumeric() == False):
			return 'Error: Numbers must only contain digits.'

		#for each math problem there's a width, equal to 2 + len(longest number)	
		if (len(final[i][0]) > len(final[i][2])):
			width = 2 + len(final[i][0])
		else:
			width = 2 + len(final[i][2])
		
		#insert spaces before the top number, len(v) == 4
		final[i].insert(0, ' ' * (width - len(final[i][0])))
		#insert spaces after the top number, except for the last problem, len(v) == 5
		if i != len(problems) - 1:
			final[i].insert(2, '    ')
		else:
			final[i].insert(2, '\n')
		#insert spaces between operand and bottom number, len(v) == 6
		final[i].insert(4, ' ' * (width - 1 - len(final[i][4])))
		#insert spaces after the bottom number, except for the last problem, len(v) == 7
		if i != len(problems) - 1:
			final[i].append('    ')
		else:
			final[i].append('\n')
		#insert dashes, len(v) == 8
		final[i].append('-' * width)
		#insert space after dashes, len(v) == 9
		if i != len(problems) - 1:
			final[i].append('    ')
		else:
			final[i].append('')
		#insert optional solutions
		if solutions == True:
			#calculate solution and turn it into a string, len(v) == 10
			if final[i][4] == '+':
				final[i].append(str(int(final[i][1]) + int(final[i][5])))
			else:
				final[i].append(str(int(final[i][1]) - int(final[i][5])))
			#insert space before solution, len(v) == 11
			final[i].insert(9, ' ' * (width - len(final[i][9])))
			#insert space after solution, len(v) == 12
			if i != len(problems) - 1:
				final[i].append('    ')
			else:
				final[i].append('')

	#populate line1
	for i in final:
		arranged_problems += i[0] + i[1] + i[2]
	#populate line2
	for i in final:
		arranged_problems += i[3] + i[4] + i[5] + i[6]
	#populate line3
	for i in final:
		arranged_problems += i[7] + i[8]
	#populate optional line4
	if solutions == True:
		arranged_problems += '\n'
		for i in final:
			arranged_problems += i[9] + i[10] + i[11]

	return arranged_problems

That’s just a part of the error. Generally in the output should be more specifically written what was returned by function and what was expected for certain input.

here’s a screenshot of the error; it’s not letting me copy from the console

Looks like returned solutions for the problems are incorrect. For example, first problem is 3 + 855, with the expected solution 858, but function returned -852 for it.

1 Like