Airthmetic formatter trouble with parsing

Tell us what’s happening:
Hello there, im stuck in the last line of my arithmetic formatter, i done all the stuff but the print of the problems that my code solve is not parsing with the format that freecodecamp requires. I dont know what is, im testing my algorith in jupyter and vscode and is going well but when i put it on the prompt of freecodecamp my code doesnt run well.

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

fNumber = list()
operator = list()
sNumber = list()

mLength = list()

el1 = []
el2 = [] 
el3 = []
el4 = []
#Handling format problems
if len(problems) > 5:
    return "Error: Too many problems."

for el in range(len(problems)):
    x = problems[el].split()
    
    if x[1] == "*" or x[1] =="/":
        return "Error: Operator must be '+' or '-'."
    try:
        x[0] = int(x[0])
        x[2] = int(x[2])
        if x[0] > 9999 or x[2] > 9999:
            return "Error: Numbers cannot be more than four digits."
    except:
            return 'Error: Numbers must only contain digits.'
    
    # only append numbers or operators allowed
    fNumber.append(x[0])
    operator.append(x[1])
    sNumber.append(x[2])
    mLength.append(max(len(str(x[0])), len(str(x[2]))))
    
    if x[1] == '+':
        el4.append(str(x[0] + x[2]).rjust(5) + '   ')
    elif x[1] == '-':
        el4.append(str(x[0] - x[2]).rjust(5) + '   ')
    
for el in fNumber:
    el1.append(str(el).rjust(5)+'   ')

count_sNumber = 0
for el in operator:
    el2.append(str(el) + str(sNumber[count_sNumber]).rjust(4) + '   ')
    count_sNumber += 1

for el in mLength:
    if el > 3:
        el3.append('-'*((el+1)) + '   ')
    else:
        el3.append('-'*((el+(5-el))) + '   ')
    
el1 = [''.join(el1[:])]
el2 = [''.join(el2[:])]
el3 = [''.join(el3[:])]
el4 = [''.join(el4[:])]

if solve == False:
    for s1,s2,s3 in zip (el1,el2,el3):
        print(s1)
        print(s2)
        print(s3)
elif solve == True:
    for s1,s2,s3,s4 in zip (el1,el2,el3,el4):
        print(s1)
        print(s2)
        print(s3)
        print(s4)

** The error**

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: None != ’ 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: None != ’ 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.


Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

Welcome to the forums, @J_rtza.

This error is saying your function’s output is None and that’s not what it expected.

This is why. Your code is using print() to print the results itself instead of return to return the strings to the caller, per the spec. print() returns None. You need to return the complete result string. Then, you can chase down any remaining formatting problems you may encounter.

Good luck.

1 Like