Arithmetic Arranger. Help with code

Hello guys!

I’m having troubles to complete the challenge of Arithmetic Arranger.

When I test my code in “main.py” I get a lot of mistakes that are not when I parse the values used in “test_module.py”.

``````def arithmetic_arranger(problems,condition=False):
import re

# SEPARO TEXTO POR ESPACIO Y VUELVO A GUARDAR
count = -1
for i in problems:
count = count + 1
problems[count] = re.split("\\s+", problems[count])

count = -1
arranged_problems = list()
l = list()

# SEPARO NUMERO SUPERIOR, INFERIOR Y TOTAL, ADEMAS PRUEBO LONGITUDES Y SI TIENEN SIGNOS QUE NO FUNCIONAN
if len(problems)>4:
print("Error: Too many problems.")
exit()

for i in problems:
count = count + 1
l = []
for j in i:
if (len(j) > 4):
print("Error: Numbers cannot be more than four digits.")
exit()
if j == "+":
pass
elif j == "-":
pass
elif j =="/":
print("Error: Operator must be '+' or '-'.")
exit()
elif j =="*":
print("Error: Operator must be '+' or '-'.")
exit()
if "+" in i:
try:
l.append(int(problems[count][0]))
l.append(int(problems[count][2]))
l.append(int(problems[count][0]) + int(problems[count][2]))
arranged_problems.append(l)
except:
print("Error: Numbers must only contain digits.")
exit()
elif "-" in i:
try:
l.append(int(problems[count][0]))
l.append((-1) * int(problems[count][2]))
l.append(int(problems[count][0]) - int(problems[count][2]))
arranged_problems.append(l)
except:
print("Error: Numbers must only contain digits.")
exit()
l2 = list()
lf = list()
for row in zip(*arranged_problems):
list(row)
for j in row:
lf.append(str(j))

last = list()
for i in range(len(problems)):
lista = list()
lista.append(lf[int(i)])
lista.append(lf[int(i + len(problems))])
lista.append(int(len(problems)) * "-")
lista.append(lf[int(i + 2 * len(problems))])
last.append(lista)

if condition is True:
for row in zip(*last):
print('{:>5}{:>10}{:>8}{:>9}'.format(*row))

return arranged_problems
``````

Could you please check it?

Thanks you!

Hi. Can you please provide a clear image of the error you got. Because in the above picture it is not seen clearly.

1 Like

Hey! It is blurred, if you click it you will be able to watch it! The problem is with the format of the printed table.

He means that your image does not show the error that you are getting. It will be easier to tell you what is wrong if you post the error message along with your code.

1 Like

Please provide a link to your project on replit, so we can actually see the error-message.
Nobody here can read your code and then just calculate the test-outputs in their head

1 Like

Sorry, I’m a newbie.

Here I send you the errors:

`````` python main.py
[[32, 698, 730], [3801, -2, 3799], [45, 43, 88], [123, 49, 172]]
F..    32         1      45      123
-698     -3801      43       49
----      ----    ----     ----
-666     -3800      88      172
F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-2/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: [[3, 855, 858], [3801, -2, 3799], [45, 43[17 chars]172]] != '    3      3801      45      123\n+ 855 [56 chars]----' : Expected different 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/boilerplate-arithmetic-formatter-2/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 arithmetic problems and a second argument of `True`.')
AssertionError: [[32, -698, -666], [1, -3801, -3800], [45[21 chars]172]] != '   32         1      45      123\n- 698 [90 chars] 172' : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.002s

FAILED (failures=2)
``````

Hm… ok so try reading the error messages, they are not that hard once you get the basics.
First line is just telling you that it is showing you previously steps (literally saying it’s just tracing back from the error)
Second is the file where the error occured
Third is the line in the file.
Fourth is the actual error-type and error-message.

``````[[3, 855, 858], [3801, -2, 3799], [45, 43[17 chars]172]] != '    3      3801      45      123\n+ 855 [56 chars]----'
``````

The line before said it is first looking at your actual output and then the expected output.
Your output:` [[3, 855, 858], [3801, -2, 3799], [45, 43[17 chars]172]]`
Expected output: `' 3 3801 45 123\n+ 855 [56 chars]----'`

Soooo you are returning a list of lists, each corresponding to a problem (first is the first calculation, second is the second one…). But that is not at all what you were supposed to do. You are supposed to arrange those problems into a text-string, as is described in the task.

1 Like

Okay! Thank you! I will try to fix it with this info. It is somewhat confusing for me because I really get a similar output with my code:

``````           32         1      45      123
-698     -3801      43       49
----      ----    ----     ----
-666     -3800      88      172
``````

It is difficult for me understanding the output format. English is not my mother language so I find it a little hard.

Thanks for your help!

You are making that output by printing. You shouldn’t print anything. You need to instead return the a string holding the output.

2 Likes

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