Help for arithmetic arranger. I don't know where I got wrong here

Here is my code for the arithmetic arranger I think i got the errors right but sometimes the alignment gets messy and I don’t know why though. Your comments will be very helpful for me. Thank you very much.

def arithmetic_arranger(problems, second_arg=None):

    upper_all = []

    lower_all = []

    lines_all = []

    ans_all = []

    if len(problems) <5:

        for item in problems:

            divided = item.split()

            upper = divided[0]

            operator = divided [1]

            lower = divided[2]

            line_length= max(len(divided[0]), len(divided[1])) + 2

            lines = ''

            for line in range(line_length):

                lines += '-'

            try:

                if len(upper) < 5 and len(lower) < 5:

                    if operator == '+':

                        answer = str(int(upper) + int(lower))

                    elif operator == '-':

                        answer = str(int(upper) - int(lower))

                    else:

                        return "Error: Operator must be '+' or '-'."

                else:

                    return "Error: Numbers cannot be more than four digits."

            except:

                return "Error: Numbers must only contain digits."

            if second_arg == True:

                upper_all.append(upper.rjust(line_length))

                lower_all.append(operator + lower.rjust(line_length - 1))

                lines_all.append(lines)

                ans_all.append(answer.rjust(line_length))

            else:

                upper_all.append(upper.rjust(line_length))

                lower_all.append(operator + lower.rjust(line_length - 1))

                lines_all.append(lines)

    else:

        return "Error: Too many problems."

    if second_arg == True:

        return f"{'   '.join(upper_all)}{chr(10)}{'   '.join(lower_all)}{chr(10)}{'   '.join(lines_all)}{chr(10)}{'   '.join(ans_all)}"

    else:

        return f"{'   '.join(upper_all)}{chr(10)}{'   '.join(lower_all)}{chr(10)}{'   '.join(lines_all)}"

Hello there.

Do you have a question?

If so, please edit your post to include it.

Learning to describe problems is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.


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 (’).

I don’t see the problem at a glance. What error messages are you getting?

I got the problem on the alignment (used the wrong index lol) but the error I still don’t know. I will print screen the failed tests

You can have up to 5 problems.

oh isee my bad. I read it wrong. lol Thank you very much!

1 Like

Still wrong though. When I check it on VS code it runs well.

It looks like a spacing issue.

I added strip but it is still not working. Here’s my VS code result.

Any change to the error messages?

None still. I’ll still try to think what to do.

The diff makes it look like the number of spaces between problems doesn’t look right.

Yep. The spaces is the problem. I counted four in the markdown but it should be five. :slight_smile: Thank you very much!

1 Like

I’d glad I could help you spot it!

Thank you very much!

1 Like