Arithmetic Formatter in:title

def AddAndSub(Array, temp):
    first_digits = list()
    operators = list()
    Second_digit = list()
    Answer = list()
    final_without_answers = list()
    final_with_answers = list()
    arranged_problems = ""
    if len(Array) >= 5:
        return "Error: Too many problems."
    for items in Array:
        if "+" in items:
            continue
        elif "-" in items:
            continue
        else:
            print("Error: Operator must be '+' or '-'.")
            return "Error: Operator must be '+' or '-'."
    for items in Array:
        if "+" in items:
            digits = items.split("+")
            if len(digits[0]) >= 5:
                print("Error: Numbers cannot be more than four digits.")
                return "Error: Numbers cannot be more than four digits."
            else:
                operators.append("+")
                first_digits.append(int(digits[0]))
                Second_digit.append(int(digits[1]))
            if len(digits[1]) >= 5:
                print("Error: Numbers cannot be more than four digits.")
                return "Error: Numbers cannot be more than four digits."
        if "-" in items:
            digits = items.split("-")
            if len(digits[0]) >= 5:
                print("Error: Numbers cannot be more than four digits.")
                return "Error: Numbers cannot be more than four digits."
            else:
                operators.append("-")
                first_digits.append(int(digits[0]))
                Second_digit.append(int(digits[1]))
            if len(digits[1]) >= 5:
                print("Error: Numbers cannot be more than four digits.")
                return
    if temp:
        count= 0
        while count < len(first_digits):
            if operators[count] == "+":
                Answer.append(first_digits[count] + Second_digit[count])
            else:
                Answer.append(first_digits[count] - Second_digit[count])
            count = count+1
    iterate = 0

    while iterate < len(first_digits):
        if temp:
            arranged_problems = "   "+str(first_digits[iterate])+"\n"+str(operators[iterate])+"   "+str(Second_digit[iterate])+"\n-----\n    "+str(Answer[iterate])
            final_with_answers.append(arranged_problems)
        else:
            arranged_problems = "   "+str(first_digits[iterate])+"\n"+str(operators[iterate])+"  "+str(Second_digit[iterate])+"\n-----\n"+str(Answer[iterate])
            final_without_answers.append(arranged_problems)
        iterate = iterate+1
    if temp:
        if len(final_with_answers) == 1:
            arranged_problems = "%s" %(final_with_answers[0])
        elif len(final_with_answers) == 2:
            arranged_problems = "%s %s" % (final_with_answers[0], final_with_answers[1])
        elif len(final_with_answers) == 3:
            arranged_problems = "%s %s %s" % (final_with_answers[0], final_with_answers[1], final_with_answers[2])
        elif len(final_with_answers) == 4:
            arranged_problems = "%4s %4s %4s %4s" % (final_with_answers[0], final_with_answers[1], final_with_answers[2], final_with_answers[3])
        elif len(final_with_answers) == 5:
            arranged_problems = "%s %s %s %s %s" % (final_with_answers[0], final_with_answers[1], final_with_answers[2],final_with_answers[3], final_with_answers[4])

return arranged_problems

The format I am getting is not meeting the requirements at all, can someone please help with formatting

Welcome, there.

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

1 Like

I just solved this one too and found myself in a similar struggle.
Maybe you can have a look at 2 things that should get you in the right direction: (apologies beforehand for the head masters kind of questions, I dont teach often)

  • Can you find a way where the final layout will be more like
'3      3003\n + 855    -    2\n-----    ------\n  858      3001'
  • What if your first_digits or second_digit has less or more than the 3 digits they have right now, will the number of ‘-’ still be the correct amount?

If it’s unclear: simply look at the Output in the readme.md and count spaces and dashes to get the exact idea.

Okay thank you so much. That was super helpful, for the amount of dashes I think its there’s a 4 digit in the same then the dashes are 6, otherwise its always 5 dashes.

not always

  5
+ 3
---