Arithmetic arranger test error

i just finished the arithmetic arranger project of scientific computing with python project .
Every rule (test condition) was working fine on my editor but when i put the code of repl.in it thew two errors which i cannot understand.

this is my code

import operator

def con_check(a):
	try:
		a = int(a)
		return True
	except:
		return False

def arithmetic_arranger(problems, con = False):
	arranged_problems = ""
	dict ={"+": operator.add,"-": operator.sub}
	first = [*range(len(problems))]
	second = [*range(len(problems))]
	sign = [*range(len(problems))]
	x = 0
	for i in problems:
		first[x], sign[x], second[x] = i.split()
		if len(str(first[x])) > 4 or len(str(second[x])) > 4:
			return "Error: Numbers cannot be more than four digits."
		elif x > 4:
			return "Error: Too many problems."
		elif not sign[x] in dict:
			print(sign[x])
			return "Error: Operator must be '+' or '-'."
		elif not con_check(first[x]) or not con_check(second[x]):
			return "Error: Numbers must only contain digits."
		x+=1
		


	# first line
	for i in range(len(problems)):
		arranged_problems += str(" "*(len(str(max(int(first[i]), int(second[i])))) - len(str(first[i])) + 2  ) + first[i] + "    ")
	
	arranged_problems += str("\n")
	#second line
	for i in range(len(problems)):
		arranged_problems += str(sign[i] + " " * (len(str(max(int(first[i]), int(second[i])) )) - len(second[i]) +1) + second[i] +"    ")
	arranged_problems += str("\n")
	#Third line
	for i in range(len(problems)):
		arranged_problems += str("--" + "-" * (len(str(max(int(first[i]), int(second[i]))))) +"    ")
	arranged_problems += str("\n")

	if con :
		for i in range(len(problems)):
			arranged_problems += str(" " * (len(str(max(int(first[i]), int(second[i])))) - len(str(dict[sign[i]](int(first[i]), int(second[i])))) + 2)  + str(dict[sign[i]](int(first[i]), int(second[i]))) + "    ")

	return arranged_problems

And this is these are the two errors repl.in is throwing

..F..
=================================================
=====================
FAIL: test_arrangement (test_module.UnitTests)
-------------------------------------------------
---------------------
Traceback (most recent call last):
  File "/home/runner/fcc-arithmetic-arranger/test
_module.py", line 10, in test_arrangement
    self.assertEqual(actual, expected, 'Expected 
different output when calling "arithmetic_arrange
r()" with ["3 + 855", "3801 - 2", "45 + 43", "123
 + 49"]')
AssertionError: '    [23 chars]  123    \n+ 855  
  -    2    + 43    +  49   [37 chars]  \n' != ' 
   [23 chars]  123\n+ 855    -    2    + 43    + 
 49\n-----[23 chars]----'
-     3      3801      45      123    
?                                 ----
+     3      3801      45      123
- + 855    -    2    + 43    +  49    
?                                 ----
+ + 855    -    2    + 43    +  49
- -----    ------    ----    -----    
?                                 -----
+ -----    ------    ----    ----- : Expected dif
ferent output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/fcc-arithmetic-arranger/test_module.py", line 39, in test_solutions
    self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.')
AssertionError: '   3[23 chars]  123    \n- 698    - 3801    + 43    +  49   [73 chars]    ' != '   3[23 chars]  123\n- 698    - 3801    + 43    +  49\n-----[57 chars] 172'
-    32         1      45      123    
?                                 ----
+    32         1      45      123
- - 698    - 3801    + 43    +  49    
?                                 ----
+ - 698    - 3801    + 43    +  49
- -----    ------    ----    -----    
?                                 ----
+ -----    ------    ----    -----
-  -666     -3800      88      172    ?                                 ----
+  -666     -3800      88      172 : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.005s

FAILED (failures=2)

Thanks to help in advance!!

Hello there,

Those errors originate from the fCC tests. It is not an issue with Python, but is saying that your script does not output the expected result. Something in your script is not outputting the correct result.

The best thing for you to do is run over the requirements again, and test your function with related arguments to see where it differs.

Also, I have moved this to a more suitable subforum
Hope this helps

Thanks for responding,
But I have checked all the requirements multiple times and the code is also giving all the output correctly.
All my outputs (talking about print not return statement) are exactly same as the the test expectation.

Once again thanks for responding :slight_smile:

have you added also the results of the operations? you should just print the given numbers as required, not add other numbers

i have just added the numbers if the second condition is given True just as said in the question.

just figured out the error .
I was adding the β€œ\n” after every line , including the last line because of which test was throwing error.

β€œ\n” or any space should not be printed in the last line where it is not needed. so i just added some condition statements to carefully place the β€œ\n” and space.

And yess ALL the test passes.

for someone who is curious I just compared the len() of the expected return from the test_module.py with my function return and found my function return to be longer then expected .

Thanks to everyone who responded.

1 Like

Can you please explain these lines? I am a newbie…

ohh , why not

    first = [*range(len(problems))]
	second = [*range(len(problems))]
	sign = [*range(len(problems))]

with range() function i am just making an array of the same length of as that of the problems input.
and the * before the range function tell the interpreter to expand the range() function as an array and with the * the array will output the range() function instead of the required array.

happy to help :blush:

Hi Aditya,

I have a quick question. Do you mind elaborating your error around β€œ\n” ? I am running into the same issue . How was the solution after fixing that?

Thanks,
Allison

you need to remove the last β€˜\n’ because the result does not have a new line. you can use pop() or change the codes before the last line

1 Like

Gotcha! Thanks a ton!