Arithmetic arrager - what now?

Tell us what’s happening:
I don’t really understand the issue with my code. It prints nicely formatted equations.

Your code so far

def arithmetic_arranger(equations, *boolean):

    assert len(equations) <= 5, "Error: Too many problems."

    upper_half_list = []
    lower_half_list = []
    line_list = []
    result_list =[]

    for equation in equations:
        content = equation.split()
        operator = content[1]

        assert len(content[0]) <= 4, "Error: Numbers cannot be more than four digits."
        assert len(content[2]) <= 4, "Error: Numbers cannot be more than four digits."

        try:
            number_1 = int(content[0])
            number_2 = int(content[2])

        except:
            print("Error: Numbers must only contain digits.")
            break

        assert operator == "+" or operator == "-", "Error: Operator must be \"+\" or \"-\"."

        if True in boolean:
            if operator == "+":
                result = str(number_1 + number_2)

            elif operator == "-":
                if number_1 < number_2:
                    result = '- ' + str(- (number_1 - number_2))

                else:
                    result = str(number_1 - number_2)
        else:
            result = ""


        if len(str(number_1)) < len(str(number_2)) + 2:
            space = len(str(number_2)) + 2 - len(str(number_1))
            line = (len(str(number_2)) + 2) * '-'
            upper_half = space * ' ' + str(number_1)
            lower_half = operator + ' ' + str(number_2)

        else:
            space = len(str(number_1)) - len(str(number_2))
            line = (len(str(number_1)) + 1) * '-'
            upper_half = ' ' + str(number_1)
            lower_half = operator + space * ' ' + str(number_2)

        result = (len(line) - len(result)) * ' ' + result
        problem = upper_half + '\n' + lower_half + '\n' + line + '\n' + result

        upper_half_list.append(upper_half)
        lower_half_list.append(lower_half)
        line_list.append(line)
        result_list.append(result)


    if len(equations) == 0:
        print("No equations!")

    elif len(equations) == 1:
        print("{0}".format(upper_half_list[0]))
        print("{0}".format(lower_half_list[0]))
        print("{0}".format(line_list[0]))
        print("{0}".format(result_list[0]))

    elif len(equations) == 2:
        print("{0}    {1}".format(upper_half_list[0], upper_half_list[1]))
        print("{0}    {1}".format(lower_half_list[0], lower_half_list[1]))
        print("{0}    {1}".format(line_list[0], line_list[1]))
        print("{0}    {1}".format(result_list[0], result_list[1]))

    elif len(equations) == 3:
        print("{0}    {1}    {2}".format(upper_half_list[0], upper_half_list[1], upper_half_list[2]))
        print("{0}    {1}    {2}".format(lower_half_list[0], lower_half_list[1], lower_half_list[2]))
        print("{0}    {1}    {2}".format(line_list[0], line_list[1], line_list[2]))
        print("{0}    {1}    {2}".format(result_list[0], result_list[1], result_list[2]))

    elif len(equations) == 4:
        print("{0}    {1}    {2}    {3}".format(upper_half_list[0], upper_half_list[1], upper_half_list[2], upper_half_list[3]))
        print("{0}    {1}    {2}    {3}".format(lower_half_list[0], lower_half_list[1], lower_half_list[2], lower_half_list[3]))
        print("{0}    {1}    {2}    {3}".format(line_list[0], line_list[1], line_list[2], line_list[3]))
        print("{0}    {1}    {2}    {3}".format(result_list[0], result_list[1], result_list[2], result_list[3]))

    elif len(equations) == 5:
        print("{0}    {1}    {2}    {3}    {4}".format(upper_half_list[0], upper_half_list[1], upper_half_list[2], upper_half_list[3], upper_half_list[4]))
        print("{0}    {1}    {2}    {3}    {4}".format(lower_half_list[0], lower_half_list[1], lower_half_list[2], lower_half_list[3], lower_half_list[4]))
        print("{0}    {1}    {2}    {3}    {4}".format(line_list[0], line_list[1], line_list[2], line_list[3], line_list[4]))
        print("{0}    {1}    {2}    {3}    {4}".format(result_list[0], result_list[1], result_list[2], result_list[3], result_list[4]))

EDIT: readibility

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0

Challenge: Arithmetic Formatter

Link to the challenge:
https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter

What errors in tests are you getting? They usually give some indication what to look at.


FAILED test_module.py::test_template[test_two_problems_arrangement1] - Assert…
FAILED test_module.py::test_template[test_two_problems_arrangement2] - Assert…
FAILED test_module.py::test_template[test_four_problems_arrangement] - Assert…
FAILED test_module.py::test_template[test_five_problems_arrangement] - Assert…
FAILED test_module.py::test_template[test_too_many_problems] - AssertionError…
FAILED test_module.py::test_template[test_incorrect_operator] - AssertionErro…
FAILED test_module.py::test_template[test_too_many_digits] - AssertionError: …
FAILED test_module.py::test_template[test_only_digits] - IndexError: list ind…
FAILED test_module.py::test_template[test_two_problems_with_solutions] - Asse…
FAILED test_module.py::test_template[test_five_problems_with_solutions] - Ass…


But the outputs look exactly how they should, e.g.

That seems a little odd… As in it will always be true…

You are using ‘print’ rather than returning a string from a function…

that was working if there was no input after the list of equations, but you’re right - changed it a bit

If you look in your ‘main.py’ in the boilerplate code there is the line:
print(arithmetic_arranger([“32 + 698”, “3801 - 2”, “45 + 43”, “123 + 49”]))

That shows you that arithmetic_arranger is expected to return a printable string. It needs to have a return and the name of a string that you’ve assembled at the end of the arithmetic_arranger function.

The tests will be looking for the same string and checking that your string matches the one the tester is expecting.

do not assert, return the string

do not print, return

same here

Looks like I need to pay for it to be visible :stuck_out_tongue:

you can link to the script, just don’t put it on a line on its own
something like: scritp here: script link all on the same line

I meesed a bit with the code - made returns from prints and now I got 4/10 passed (the conditions for arguments), but still the equations look like a mess in console on replit.com

Here is new code:

 def arithmetic_arranger(equations, boolean = False):
 
    if len(equations) > 5:
        return "Error: Too many problems."

    upper_half_list = []
    lower_half_list = []
    line_list = []
    result_list =[]

    for equation in equations:
        content = equation.split()
        operator = content[1]


        if len(content[0]) > 4:
            return "Error: Numbers cannot be more than four digits."
          
        if len(content[2]) > 4:
            return "Error: Numbers cannot be more than four digits."

        try:
            number_1 = int(content[0])
            number_2 = int(content[2])

        except:
            return "Error: Numbers must only contain digits."
        
        if operator != "+" and operator != "-":
            return "Error: Operator must be \'+\' or \'-\'."

        if boolean == True:
            if operator == "+":
                result = str(number_1 + number_2)

            elif operator == "-":
                if number_1 < number_2:
                    result = '- ' + str(number_2 - number_1)

                else:
                    result = str(number_1 - number_2)
        elif boolean == False:
            result = ""

        if len(str(number_1)) < len(str(number_2)) + 2:
            space = len(str(number_2)) + 2 - len(str(number_1))
            line = (len(str(number_2)) + 2) * '-'
            upper_half = space * ' ' + str(number_1)
            lower_half = operator + ' ' + str(number_2)

        else:
            space = len(str(number_1)) - len(str(number_2))
            line = (len(str(number_1)) + 1) * '-'
            upper_half = ' ' + str(number_1)
            lower_half = operator + space * ' ' + str(number_2)

        result = (len(line) - len(result)) * ' ' + result
        
        upper_half_list.append(upper_half)
        lower_half_list.append(lower_half)
        line_list.append(line)
        result_list.append(result)
      
    distance = "    "

    top = distance.join(upper_half_list)
    top_middle = distance.join(lower_half_list)
    bottom_middle = distance.join(line_list)
    bottom = distance.join(result_list)
    
    arranged_problems = top + '\n' + top_middle + '\n' + bottom_middle + '\n' + bottom

    return arranged_problems

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

SCRIPT

looking at the output
you don’t have the right number of spaces between problems

image


It’s unfortunately a mystery to me…
On my PC I run this code with addition of

print(arithmetic_arranger(['1 + 1111', '11 - 111', '111 + 11', '1111 + 11'], True))

to test output and everything looks brilliant :confused:

The number of spaces between each problem isn’t right… It doesn’t match the required number.

I think you’re a space short on the 3rd and 4th sums…

See the question:

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

Note the alignment of 123. It’s two spaces back from the ‘+’ on the line below, not one like your screenshot.

(It took me a while to get the spaces ‘just right’ - it is very fussy)

Thank you all, managed to get it to work.
For those curious - I’ve been printing the result even when there was no True argument, but as it was only spaces (blank) I didn’t see it until I changed all spaces into ‘x’ :stuck_out_tongue:
Second thing - last test - I misinterpreted the examples and tried to put a space between result number and the ‘-’ if there was any. Deleted that part and voila.

Love y’all!!!

Congrats!
I had to fight the spaces too.