Help with the Arithmetic Formatter project

Hello! sorry for the repetitive subject and the bad English! but I couldn’t find help elsewhere , so I was done with the Arithmetic Formatter project, my code works well outside of the Freecodecamp console and even inside of it , yet it keep showing me error.

this is my code :

#!python3

def arithmetic_arranger(problems, show_answers=False):
	if len(problems) > 5:
		return 'Error: Too many problems.'

	cond_1 = map(lambda x : x.split()[1], problems)
	if set(cond_1) != {'+', '-'}:
		return "Error: Operator must be '+' or '-'."

	cond_2 = map(lambda x : x.split(), problems)
	for item in cond_2:
		if(item[0].isdigit() and item[2].isdigit()) is False:
			return 'Error: Numbers must only contain digits.'
		if len(item[0]) > 4 or len(item[2]) > 4:
			return 'Error: Numbers cannot be more than four digits.'

	first = ''
	second = ''
	third = ''
	fourth = ''

	for problem in problems:
		width = max(len(problem.split()[0]), len(problem.split()[2]))
		first_Num = problem.split()[0].rjust(width+2)
		second_Num = f'{problem.split()[1]} {problem.split()[2].rjust(width)}'
		_dashes = '-' * (width +2)
		if problem.split()[1] == '+':
			result = str(int(problem.split()[0]) + int(problem.split()[2])).rjust(width+2)
		if problem.split()[1] == '-':
			result = str(int(problem.split()[0]) - int(problem.split()[2])).rjust(width+2)
		if problems != problems[-1]:
			first += first_Num + '    '
			second += second_Num + '    '
			third += _dashes + '    '
			fourth += result + '    '
		else:
			first += first_Num
			second += second_Num
			third += _dashes
			fourth += result

	if show_answers :
		res = first + '\n' + second + '\n' + third + '\n' + fourth
	else:
		res = first + '\n' + second + '\n' + third 
	return res

print(arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49", "80 + 30"]))

this is result inside of Freecodecamp console :
Screenshot 2024-07-05 145413

this is the result outisde of freecodecamp :
Screenshot 2024-07-05 145609

this is the only error I get :

thank you in advance for all the tips!

please always post a link to the step/project/challenge when you ask for help. Can you do that now please?


have you looked at the browser console? it gives a more verbose test result

yes sure! sorry about that! this is the link to the challenge :

this is another photo from the Freecodecamp console the code seems to meet all the requirements
image

sorry here it is :

You seem to have overlooked this line

Note: open the browser console with F12 to see a more verbose output of the tests.

If I run your code, I can see that, there is no need for a screenshot.

That is the full output of the tests, from there you can see the exact difference between your output and teh expected output

yeah thanks for your help, the code I wrote outputs the problems outlined as the challenge require it to be.

no, it doesn’t, as you can infer from the test failing. There you can see the exact difference

image

I still can’t see the problem since this is the output I get on the Freecodecamp console as well, if I try to tweak the spacing and stuff, it’s no longer aligned, thanks anyway I’ll keep trying.

this is from the screenshot you posted, it shows the difference between your code and the expected:
image

You have more characters than needed.

Also the assertion error.

AssertionError: '  1         1    \n+ 2    - 9380    \n---    ------    '
             != '  1         1\n+ 2    - 9380\n---    ------'

It’s all there for you to look at exactly for this reason.

can you please hint out which lines of code I should modify ? I can’t find the error :frowning:

which lines add four spaces?

1 Like

you are comparing a list with a string

1 Like

damn, I did not pay attention to that one! added that ‘s’ by mistake , thank you a lott! now only one error left to fix

thank you a lot ! it’s fixed now! I did not pay attention to “problems” and “problem” , the intension was to compare problem to problems[-1]

thank you a lot again :heart: :pray:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.