# Help needed w/ my 1st project | Arithmetic-formatter

Hello Folks,

I´ve been working on the freecode camp Arithmetic Formatter lately. This is the first time Im doing a project this tediuos and long. I´ve been having all type of issues which I have solved. However, I cannot figure this out. I searched on other topics, but since the code is different I cannot see what the write way would be without changing the whole code. I feel like I either fail when arranging the spaces or when I say “If show_results == True: …”

I would appreciate any hint on this!

Thank you so much

``````import operator
import re

def arithmetic_arranger(problems, show_results = False):

### Diferent lines to print
arranged_problems = ""
line1 = list()
line2 = list()
line3 =list()
line4 = list()

# Limit of 5 problems solution
if len(problems) > 5:
return "Error: Too many problems."

# Looking for appropiate operators
for items in problems:
if "/" in items:
return "Error: Operator must be '+' or '-'."
if "x" in items:
return "Error: Operator must be '+' or '-'."
else:
continue

# Looking for letters

for item in problems:
letter = re.findall("[A-Za-z]+", item)
if len(letter) > 0 :
return "Error: Numbers must only contain digits."

# Max 4 digits

for items in problems:
item = items.split()
if len(item[0]) >4 or len(item[2]) >4:
return "Error: Numbers cannot be more than four digits."

# Turning strings into integers and oinf the calculations
results = []
operators = {"+" : operator.add, "-" : operator.sub}
for items in problems:
item = items.split()
i1, i2 = int(item[0]), int(item[2])
oper = item[1]
results.append((operators[oper](i1,i2)))

#### Line 3

for items in problems:
item = items.split()
if len(item[0]) > len(item[2]):
line3.append("_" * (len(item[0])+2))
elif len(item[2]) > len(item[0]):
line3.append("_" * (len(item[2])+2))
elif len(item[0]) == len(item[2]):
line3.append("_" * (len(item[2])+2))

### Linea 4
for dashes, numbers in zip(line3, results):
line4.append( (" " * (len(dashes) - len(str(numbers))) +  f"{numbers}"))

### Line 1

for items, dashes in zip(problems, line3):
item = items.split()
line1.append ( " " * ( len(dashes) - len(item[0])) + item[0])

#Line 2

for items, dashes in zip(problems, line3):
item = items.split()
simbol = ""
if "+" in item:
simbol = "+"
if "-" in item:
simbol = "-"
line2.append( simbol + " " * ( len(dashes) - len(item[2])-1) + item[2])

# Turning lists into strings

string_1 = "    ".join([str(element) for element in line1])

string_2 = "    ".join([str(element) for element in line2])

string_3 = "    ".join([str(element) for element in line3])

string_4 = "    ".join([str(element) for element in line4])

string_1.rstrip()
string_2.rstrip()
string_3.rstrip()
string_4.rstrip()

if show_results == True:
arranged_problems = string_1.rstrip() + "\n" + string_2.rstrip() + "\n" + string_3.rstrip() + "\n" + string_4.rstrip()
return arranged_problems

else:
arranged_problems = string_1.rstrip() + "\n" + string_2.rstrip() + "\n" + string_3.rstrip()
return arranged_problems

``````

Whenever I run it I got the following errors:

``````python main.py
32      3801      45      123
+ 698    -    2    + 43    +  49
_____    ______    ____    _____
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/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: '    [34 chars]5    -    2    + 43    +  49\n_____    ______    ____    _____' != '    [34 chars]5    -    2    + 43    +  49\n-----    ------    ----    -----'
3      3801      45      123
+ 855    -    2    + 43    +  49
- _____    ______    ____    _____+ -----    ------    ----    ----- : 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/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: '   3[59 chars] 49\n_____    ______    ____    _____\n -666  [21 chars] 172' != '   3[59 chars] 49\n-----    ------    ----    -----\n -666  [21 chars] 172'
32         1      45      123
- 698    - 3801    + 43    +  49
- _____    ______    ____    _____
+ -----    ------    ----    -----
-666     -3800      88      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.003s

FAILED (failures=2)

``````

The link to the problem is: [Arithmetic Formatter - Free Code Camp] https://replit.com/@alfonsosmdc/boilerplate-arithmetic-formatter#arithmetic_arranger.py

Failing tests suggests that when everything is fine with the input, function returns just few underscores. Do you know why that’s happening?

1 Like

Hey,

I have no idea of what is happening… I´ve made some changes and it looks like I am a bit closer. Actually, what is driving me crazy is the fact that whenever I run it on VSCode it works. I pass every test the test_module is supposed to do.

``````import operator
import re

def arithmetic_arranger(problems, show_results = False):

### Diferent lines to print
arranged_problems = ""
line1 = list()
line2 = list()
line3 =list()
line4 = list()

# Limit of 5 problems solution
if len(problems) > 5:
return "Error: Too many problems."

# Looking for appropiate operators
for items in problems:
if "/" in items:
return "Error: Operator must be '+' or '-'."
if "x" in items:
return "Error: Operator must be '+' or '-'."
else:
continue

# Looking for letters

for item in problems:
letter = re.findall("[A-Za-z]+", item)
if len(letter) > 0 :
return "Error: Numbers must only contain digits."

# Max 4 digits

for items in problems:
item = items.split()
if len(item[0]) >4 or len(item[2]) >4:
return "Error: Numbers cannot be more than four digits."

# Turning strings into integers and oinf the calculations
results = []
operators = {"+" : operator.add, "-" : operator.sub}
for items in problems:
item = items.split()
i1, i2 = int(item[0]), int(item[2])
oper = item[1]
results.append((operators[oper](i1,i2)))

#### Line 3

for items in problems:
item = items.split()
if len(item[0]) > len(item[2]):
line3.append("_" * (len(item[0])+2))
elif len(item[2]) > len(item[0]):
line3.append("_" * (len(item[2])+2))
elif len(item[0]) == len(item[2]):
line3.append("_" * (len(item[2])+2))

### Linea 4
for dashes, numbers in zip(line3, results):
line4.append( (" " * (len(dashes) - len(str(numbers))) +  f"{numbers}"))

### Line 1

for items, dashes in zip(problems, line3):
item = items.split()
line1.append ( " " * ( len(dashes) - len(item[0])) + item[0])

#Line 2

for items, dashes in zip(problems, line3):
item = items.split()
simbol = ""
if "+" in item:
simbol = "+"
if "-" in item:
simbol = "-"
line2.append( simbol + " " * ( len(dashes) - len(item[2])-1) + item[2])

# Turning lists into strings

string_1 = "    ".join([str(element) for element in line1])

string_2 = "    ".join([str(element) for element in line2])

string_3 = "    ".join([str(element) for element in line3])

string_4 = "    ".join([str(element) for element in line4])

if show_results == True:
arranged_problems = string_1.rstrip() + "\n" + string_2.rstrip() + "\n" + string_3.rstrip() + "\n" + string_4.rstrip()
return(arranged_problems)

else:
arranged_problems = string_1.rstrip() + "\n" + string_2.rstrip() + "\n" + string_3.rstrip()
return(arranged_problems)

#Main

print(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))
print(arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]))
print(arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"]))
print(arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"]))
print(arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"]))
print(arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"]))
arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True)
``````

And this is what VSCode throws to me

``````    3      3801      45      123
+ 855    -    2    + 43    +  49
_____    ______    ____    _____
11      3801      1      123         1
+  4    - 2999    + 2    +  49    - 9380
____    ______    ___    _____    ______
Error: Too many problems.
Error: Operator must be '+' or '-'.
Error: Numbers cannot be more than four digits.
Error: Numbers must only contain digits.
32         1      45      123
- 698    - 3801    + 43    +  49
_____    ______    ____    _____
-666     -3800      88      172
``````

An as I said before, it runs all the tests and show all the string well placed and with the corresponding dashes

Perhaps you forgot to update version on replit after making changes locally?

1 Like

I have no idea about how to do that, but i will investigate and try it . Thanks!

One of the way would be just copying whole contents of the file from vs code to the appropriate file in the editor on replit.

1 Like

Just did it and I got the same error Lol

I’m looking at the code here: https://replit.com/@alfonsosmdc/boilerplate-arithmetic-formatter#arithmetic_arranger.py and code there still differs in few important places to the code that’s in your post with what vscode ouputs.

1 Like

it´s great you can see them . Would you mind copy/paste here some so I could see them? The more I check it the more clueless I am . I´d appreciate it a lot.

Just realized about the returns I worte for line1, line2 and so on. Looks like I´m a bit closer to figure it out . Thanks !!!

At first I’d suggest updating code linked on replit, it’s just more convenient for those helping to refer to it, rather than needing to copy and paste code from here to run it somewhere else.

Just did it. Sorry , I thought it would automatically update and show whatever code was there, this is my first time using replit …

Looking now at the tests output. Part of it below is formatted that function output that doesn’t match starts with `-`, and what is expected starts with `+`.

``````     32         1      45      123
- 698    - 3801    + 43    +  49
- _____    ______    ____    _____
+ -----    ------    ----    -----
``````
1 Like

Just checked that out, and what I actually see is that it show as well as the right one an extra row with the wrong operator and the dashes as well. I will try to code agaain the part in which I use the method join() and format to see if the problem is there! Again, thanks for the hints!!! To be the first project, it is driving my crazy

I think you might be missing the parenthesis on the return statements

Should be:

``````return ("Error: Too Many Problems")
``````

Please don’t necro a 5 month old threat…
Also please don’t give out advice if you are not sure if those are relevant. Because `return` doesn’t need parenthesis.

1 Like

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