Need Help In Arthmetic Formatter

Tell us what’s happening:
I have been working on this project for a while right now, and I finally got positive results by the end while debugging, and trying it myself with multiple string arrays . But still, for some reason I get three failures when I try to submit it with the inbound testing method.

I tried to read from other people’s posts in case i was doing something wrong or having a wrong understanding of the topic but I didn’t find anything wrong.

Thank you :slightly_smiling_face: .

Your code so far
def arithmetic_arranger(problems, show_answer=False):

if len(problems) >= 6:
	return 'Error: Too many problems.'

lower = ''
upper = ''
line = ''
downer = ''
answer = ''

for problem in problems:

	problem.split()
	first_number = problem.split()[0]
	operation = problem.split()[1]
	second_number = problem.split()[2]

	if first_number.isnumeric() or second_number.isnumeric():
			
		if len(first_number) >= 5 or len(second_number) >= 5:
			return "Error: Numbers cannot be more than four digits."

		if operation == '-' or operation == '+':

			if len(str(first_number)) > len(str(second_number)):
				upper += '  ' + first_number + '    '
			elif len(str(first_number)) < len(str(second_number)):
				upper += '  ' + (' ' * abs(len(str(first_number)) - len(str(second_number)))) + first_number + '    '
			else:
				upper += '  ' + first_number + '    '

			if len(str(first_number)) > len(str(second_number)):
				lower += operation + ' ' + (' ' * abs(len(str(first_number)) - len(str(second_number)))) + second_number + '    '
			elif len(str(first_number)) < len(str(second_number)):
				lower += operation + ' ' + second_number + '    '
			else:
				lower += operation + ' ' + second_number + '    '

			line += '-' * (max(len(str(first_number)), len(str(second_number))) + 2) + '    '

			if show_answer:
				if operation == '-':
					answer += '  ' + (' ' * (max(len(str(first_number)), len(str(second_number))) - len(str(int(first_number) + int(second_number))))) + str(int(first_number) - int(second_number)) + '    '
				else:
					answer += '  ' + (' ' * (max(len(str(first_number)), len(str(second_number))) - len(str(int(first_number) + int(second_number))))) + str(int(first_number) + int(second_number)) + '    '


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

if show_answer:
	final = upper + '\n' + lower + '\n' + line + '\n' + answer
	return final

else:
	final = upper + '\n' + lower + '\n' + line
	return final

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

Hello and welcome to the FCC community~!

Can you provide us with the link to your repl.it (or wherever you are running this, assuming you’re not running it locally)? Being able to view the actual error message would help us debug your issue. :slight_smile:

Perfect!

Okay, the second error is saying your code doesn’t check for inputs that contain non-number characters. The test passes “98 + 3g5” as a problem, and your code accepts and formats that instead of returning an error. It looks like you have that error message in your code, so I would double check the logic because the error is not generating when it should.

The first and third errors appear to be caused by extra trailing whitespace at the end of your lines. The error printouts are notoriously difficult to read (I had a lot of trouble with them myself), but from what I can tell this looks like the issue.

1 Like

Thank you so much for your help. I had fixed the problems you had mentioned in your reply. One failure was fixed but the other two are still wrong.

Nice work!

I tried taking another look, but it appears you may be actively editing it as I got a syntax error on the last line. :confused:

Feel free to @ me if you need help reading the new error messages! :smiley:

1 Like