Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code is working and displaying errors on the test data supplied when i am passing it into the function, but the Run test cases are failing for the same.

Your code so far

def arithmetic_arranger(problems, show_answers=True):
    line1 = []
    line2 = []
    line3 = []
    line4 = []

    for k in problems:
        if '+' in k:
            parts = k.split('+')
            operator = '+'
        elif '-' in k:
            parts = k.split('-')
            operator = '-'
        else:
            continue

        left = parts[0].strip()
        right = parts[1].strip()
        width = max(len(left), len(right)) + 2

        line1.append(left.rjust(width))
        line2.append(operator + right.rjust(width - 1))
        line3.append('-' * width)

        if show_answers:
            result = str(eval(k))
            line4.append(result.rjust(width))

    print('\t'.join(line1))
    print('\t'.join(line2))
    print('\t'.join(line3))
    if show_answers:
        print('\t'.join(line4))
    


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

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

what is your function returning?

Function returns the desired output format when passing the correct input values -

no, it returns None, see the last line in the terminal? that’s from the print that contains the function call

the rest is printed by the prints inside the function

removing the last line i.e. -
print(f’\n{arithmetic_arranger([“32 + 698”, “3801 - 2”, “45 + 43”, “123 + 49”])}')

still does not solves the issue of failing tests

that line is useful, it’s showing the issue

just removing the line does not just that your function returns None, not a string as requested

Let me know what i can explore further, as keeping or removing the line doesn’t really change anything.

that line allows you to see what your function is returning

you need to change what your function is returning, as at this time it does not return anything

Not sure why the tests are failing. when i am calling the function with the given test data as in the test cases , i am getting the error returned as you can see in the screenshot

Tweaked the function and now i am able to return the errors rather printing the errors within the function. But now currently stuck at returning the correct formatted problems, Although my output is generating the desired result but the tests are not passing.

please share your code in a post, not a screenshot

def arithmetic_arranger(problems, show_answers=False):
    line1 = []
    line2 = []
    line3 = []
    line4 = []
    er=0
    if len(problems)>5:
        return('Error: Too many problems.')
        
    for k in problems:
        if '+' in k:
            parts = k.split('+')
            n = [item.strip() for item in parts]
            if   n[0].isdigit() and n[1].isdigit():
                pass
            else:
                return("Error: Numbers must only contain digits.")
                er=1
                break
            operator = '+'
        elif '-' in k:
            parts = k.split('-')
            n = [item.strip() for item in parts]
            if  n[0].isdigit() and n[1].isdigit():
                pass
            
            else:
                return("Error: Numbers must only contain digits.")
                er=1
                break
            operator = '-'
        else:
            er=1
            return("Error: Operator must be '+' or '-'.")
            break

        left = parts[0].strip()
        right = parts[1].strip()
        width = max(len(left), len(right)) + 2
        if len(left)>4 or len(right)>4:
            return('Error: Numbers cannot be more than four digits.')
            er=1
            break
        line1.append(left.rjust(width))
        line2.append(operator + right.rjust(width - 1))
        line3.append('-' * width)

        if show_answers:
            result = str(eval(k))
            line4.append(result.rjust(width))
    if er==0:
            a='\t'.join(line1)
            b='\t'.join(line2)
            c='\t'.join(line3)
    if show_answers:
            e= '\t'.join(line4)
            
            d=a+'\n'+b+'\n'+c+'\n'+e
            return(d)
    else:
            d=a+'\n'+b+'\n'+c
            return(d)
            
            

#print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')

#print(f'\n{arithmetic_arranger(["3 + 855", "988 + 40"],True)}')

arithmetic_arranger(["3801 - 2", "123 + 49"],True)

there are some issues

if you use

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

to see what the first test function call returns you get:
' 3801\t 123\n- 2\t+ 49\n------\t-----', which has quite a few differences from the desired output

you will need to keep working on this