Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Hi,
I’m wondering to know what’s wrong in my code for the 1st projet “Build Arithmetic formatter” of the curriculum “Scientific Computing with Python”.
All tests failed but this check:
print(arithmetic_arranger([“32 - 698”, “1 - 3801”, “45 + 43”, “123 + 49”, “988 + 40”], True)==" 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028") → True
Thanks for helps

Your code so far

def arithmetic_arranger(problems, show_answers=False):
   #print("Hello world!")
    checkProblemsLength(problems)
    up = ''
    middle = ''
    dashes = ''
    result = ''
    for prob in problems:
        tab = prob.split(' ')
        checkOperand(tab[0])
        checkOperand(tab[2])
        checkOperandLen(tab[0])
        checkOperandLen(tab[2])
        checkOperator(tab[1])
        output = displayOperation(tab[0], tab[1], tab[2])
        up += output["operand1"] + '    '
        middle += output["operand2"] + '    '
        dashes += output["dashes"] + '    '
        result += output["result"] + '    '

    if show_answers:
        out = up.rstrip()+"\\n"+middle.rstrip()+"\\n"+dashes.rstrip()+"\\n"+result.rstrip()
    else:
        out = up.rstrip()+"\\n"+middle.rstrip()+"\\n"+dashes.rstrip()
    return out
# Check values
def checkProblemsLength(probs):
    if len(probs) > 5:
        raise ValueError('Error: Too many problems.')

def checkOperandLen(operand):
    if len(operand) > 4:
        raise ValueError('Error: Numbers cannot be more than four digits.')

def checkOperand(operand):
    if not operand.isdigit():
        raise ValueError('Error: Numbers must only contain digits.')

def checkOperator(operator):
    if not operator == '+' and not operator == '-':
        raise ValueError("Error: Operator must be '+' or '-'.")

def displayOperation(operand1, sign, operand2):
    output = {}
    maxLength = max(len(operand1), len(operand2))
    dashes = ''.join(['-' for x in range(0, maxLength + 2)])
    line1 = ''.join([' ' for x in range(0, maxLength + 2 - len(operand1))]) + operand1
    line2 = sign + ''.join([' ' for x in range(0, maxLength + 1 - len(operand2))]) + operand2

    output["operand1"] = line1
    output["operand2"] = line2
    output["dashes"] = dashes
    if sign == '+':
        result = int(operand1) + int(operand2)
    else:
        result = int(operand1) - int(operand2)
    
    line3 = ''.join([' ' for x in range(0, maxLength + 2 - len(str(result)))]) + str(result)
    output["result"] = line3
    return output

print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)=="   32         1      45      123      988\\n- 698    - 3801    + 43    +  49    +  40\\n-----    ------    ----    -----    -----\\n -666     -3800      88      172     1028")



 


Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

1 Like

It seems that your function is not handling all the inputs correctly. Please recheck the project requirements to ensure you account for all the necessary checks, such as rejecting invalid operators like multiplication and division. Also, verify that operands are numeric and within the four-digit limit. Additionally, ensure that the formatting is correct; for instance, the operator check should use not in ['+', '-']. Also, use rstrip() properly to eliminate any trailing spaces. Once you’ve made these adjustments, run the tests again and let us know if you still face issues.

1 Like

Thanks for your feedback!
I use these functions to check all requirements you point out.
checkProblemsLength // max
checkOperand // check if operand is digit
checkOperandLen // check length of operand
checkOperator // check if the operation sign is “+” or “-”
And checked them one by one.
Best regards!

1 Like

Please notice you have used \\n in constructing your returned output. Print the returned string out and see what it looks like.

Also the Rules of the project states that the error message should be assigned to the return value of the function, not raise a ValueError:

The function will return the correct conversion if the supplied problems are properly formatted, otherwise, it will return a string that describes an error that is meaningful to the user.

2 Likes

Thanks @SzeYeung1
I added the “\n” in my return string to be inline with expectations.
For examples: Test case #10

print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True))

My code return:

   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028

Here is my new updated. https://www.freecodecamp.org/learn/scientific-computing-with-python/build-an-arithmetic-formatter-project/build-an-arithmetic-formatter-project

Thanks for your help!

1 Like

Hello,
Thanks to all of you @SzeYeung1 @smit_007
I realize two mistakes in my code that you both point out but weren’t clear after reading your feedback. :slightly_smiling_face:
#1: I check separetely the operand and operator but I didn’t catch the return value after calling them in my main function. → To avoid it (double check) I migrate each check in main function.
#2: I added the escape ‘/’ to be inline with the test case output that does not necessary.
Everything works now!
https://www.freecodecamp.org/learn/scientific-computing-with-python/build-an-arithmetic-formatter-project/build-an-arithmetic-formatter-project
Sorry for taking your time.
Best regards.

1 Like

No problem at all! I’m happy to help anytime.