Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code is doing what it is supposed to do in the preview window but when i run tests only the tests that is needed to raise an error passes while all other tests that should preview an answer fails

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    
    #validating the limit of problems count
    if len(problems) > 5:
        return "Error: Too many problems."

    #setting up initial rows strings
    first_row = ""
    second_row = ""
    third_row = ""
    forth_row = ""

    for problem in problems:
        #validating that the problem has only 3 parts
        if len(problem.split(" ")) == 3:
            pass
        else:
            return "Error: Each problem should follow the formate (number1 +OR- number2)"
        
        number1 = problem.split(" ")[0]
        operator = problem.split(" ")[1]
        number2 = problem.split(" ")[2]

        #validating the operator sign        
        if operator not in ("+", "-"):
            return "Error: Operator must be '+' or '-'."
        
        #validating that numbers only contain digits
        if is_int(number1):
            if is_int(number2):
                pass
            else:
                return 'Error: Numbers must only contain digits.'
        else:
            return 'Error: Numbers must only contain digits.'
        
        #validating that each number contains 4 digits or less
        if len(number1) <= 4:
            if len(number2) <= 4:
                pass
            else:
                return 'Error: Numbers cannot be more than four digits.'
        else:
            return 'Error: Numbers cannot be more than four digits.'

        #adjusting formate
        if len(number1) >= len(number2):
            space = len(number1) - len(number2)
            first_row += " "*2 + number1 + " "*4
            second_row += operator + " " + " "*space + number2 + " "*4
            third_row += "--" + "-"*len(number1) + " "*4
            total = int(number1) + int(number2)
            if len(str(total)) > len(number1):
                forth_row += " " + str(total) + " "*4
            else:
                forth_row += " "*2 + str(total) + " "*4
        else:
            space = len(number2) - len(number1)
            first_row += " "*2 + " "*space + number1 + " "*4
            second_row += operator + " " + number2 + " "*4
            third_row += "--" + "-"*len(number2) + " "*4
            total = int(number1) + int(number2)
            if len(str(total)) > len(number2):
                forth_row += " " + str(total) + " "*4
            else:
                forth_row += " "*2 + str(total) + " "*4

    if show_answers:
        new_problems = first_row+'\n'+second_row+'\n'+third_row+'\n'+forth_row
    else:
        new_problems = first_row+'\n'+second_row+'\n'+third_row

    return new_problems

def is_int(number):
    if number[0] in ('-', '+'):
        return number[1:].isdigit()
    return number.isdigit()

print(f'\n{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/128.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Have you tried examine the test logs in the browser console window?

not that one, the one mentioned here

Note: open the browser console with F12 to see a more verbose output of the tests.


if this is the right one I tried to understand it but couldnt

image

the assertion error is the first interesting thing, it shows your string first, then the expected string, it’s useful to see invisible characters

then there is the diff, where lines starting with - are yours and lines starting with + are what is expected, and the lines starting with ? point out the difference

In this case you have extra characters to the right of 988

also, the last line didn’t go to a new line, but it’s showing that the expected solutions are different from the ones you have in the output string for the first two operations, where you have negative numbers

thank you so much the problem was that i added 4 spaces after each problem which did the same at the end of each row so instead I put an if conditions so that if we are at the last problem we dont add spaces