Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I checked one by one each test by hand and it’s working. I know it could probably work more efficiently, but I don’t know why I get everything wrong when I try this out. Help me please!

Your code so far


def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return print('Error: Too many problems.')
    else:
        line1 = ''  #operand1
        line2 = ''  #sign+operand2
        line3 = ''  #dashes
        line4 = ''  #result
        for problem in problems:

            op1, sign, op2 = problem.split()
            length = max(len(op1), len(op2)) + 2
            if (length - 2) > 4:
                return print('Error: Numbers cannot be more than four digits.')
            if not op1.isdigit() or not op2.isdigit():
                return print('Error: Numbers must only contain digits.')
            if sign == '+':
                total = int(op1) + int(op2)
            elif sign == '-':
                total = int(op1) - int(op2)
            else:
                return print("Error: Operator must be '+' or '-'.")
            line1 += op1.rjust(length)
            line2 += sign + op2.rjust(length - 1)
            line3 += '-' * length
            result = str(total)
            line4 += result.rjust(length)
            if problem is problem[0]:
                continue
            else:
                line1 += '    '
                line2 += '    '
                line3 += '    '
                line4 += '    '
                

        if show_answers:
            return print(f'{line1}\n{line2}\n{line3}\n{line4}')
        else:
            return print(f'{line1}\n{line2}\n{line3}')
            

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

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Return the string, don’t return a print()

To print call your function like this:

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

Cool! Now all the tests meant to raise errors are working, but still not working the ones returning numbers :frowning:

Does your output appear formatted correctly?

You can press F12 and check the browser console for a more detailed error generated by running the tests.

Finally got the error. I was adding extra spaces at the end, but sorted out. Thanks so much for the help!

1 Like