Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Can someone please tell me why the tests are not getting passed? The result showing here is exactly what it should be but I’m stuck at this point.

Your code so far

def arithmetic_arranger(problems, show_answers=False):

    line_1 = ''
    line_2 = ''
    line_3 = ''
    results = ''
  
    if len(problems) > 5:
        return 'Error: Too many problems.'

    for prob in problems:
        number = prob.split()

        operand_1 = number[0]
        operator = number[1]
        operand_2 = number[2]
        width = max(len(operand_1), len(operand_2))

        if operator == '/' or operator =='*':
            return "Error: Operator must be '+' or '-'."

        if not operand_1.isdigit() or not operand_2.isdigit():
            return'Error: Numbers must only contain digits.'
                
        if len(operand_1)>4 or len(operand_2)>4:
            return 'Error: Numbers cannot be more than four digits.'
                
        else:         
            line_1 += operand_1.rjust(width+2) + '    '
            
            line_2 += operator+' '+operand_2.rjust(width) + '    '

            line_3 += '-' * (width+2) + '    '

        if show_answers:
            if operator == '+':

                results += str(int(operand_1) + int(operand_2)).rjust(width+2) + '    '

            else:
                results += str(int(operand_1) - int(operand_2)).rjust(width+2) + '    '


    return (f'{line_1}\n{line_2}\n{line_3}\n{results}')

                

    return problems

print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)}')


Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Mobile Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Hi @ayanaich,

Test your code like this to compare what your code is returning to what is expected:

print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"])))
print(repr('   3801      123\n-    2    +  49\n------    -----'))

Happy coding!