Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

I am doing the assignment project but it’s not giving me a pass except for the error codes.

When I get them to print it shows me the exact same outputs as the test is asking, but I still don’t get pass for this.

I’ve finished the codes about a few days ago but I’m spending so many hours to get this to work, and when I press get help to create a topic the pop-up just disappears without even creating the topic so I had to just copy and paste the whole code below.


def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        err1 = 'Error: Too many problems.'
        return err1

    for i in problems:
        num = i.split()

        if not num[0].isdigit() or not num[2].isdigit():
            err2 = 'Error: Numbers must only contain digits.'
            return err2
        
        elif num[1] not in ['-', '+']:
            err3 = "Error: Operator must be '+' or '-'."
            return err3

        elif len(num[0]) > 4 or len(num[2]) > 4:
            err4 = 'Error: Numbers cannot be more than four digits.'
            return err4
    
    line1 = ''
    line2 = ''
    line3 = ''
    line4 = ''

    for i in problems:
        num = i.split()
        if len(num[0]) > len(num[2]):
            ml = len(num[0])
            line1 += (f"{int(num[0]):>{ml+2}}" + "    ")
        
        else:
            ml = len(num[2])
            line1 += (f"{int(num[0]):>{ml+2}}" + "    ")


    for i in problems:
        num = i.split()
        if len(num[0]) > len(num[2]):
            ml = len(num[0])
            line2 += (f"{num[1]}" + " " + f"{int(num[2]):>{ml}}" + "    ")
        else:
            ml = len(num[2])
            line2 += (f"{num[1]}" + " " + f"{int(num[2]):>{ml}}" + "    ")
    
    for i in problems:
        num = i.split()
        if len(num[0]) > len(num[2]):
            ml = len(num[0])
            line3 += ("-" * (ml + 2) + "    ")
            if num[1] == '+':
                a = int(num[0]) + int(num[2])
                line4 += (f"{a:>{ml + 2}}" + "    ")
                
            else:
                a = int(num[0]) - int(num[2])
                line4 += (f"{a:>{ml + 2}}" + "    ")

        else:
            ml = len(num[2])
            line3 += ("-" * (ml + 2) + "    ")
            if num[1] == '+':
                a = int(num[0]) + int(num[2])
                line4 += (f"{a:>{ml + 2}}" + "    ")
            else:
                a = int(num[0]) - int(num[2])
                line4 += (f"{a:>{ml + 2}}" + "    ")

    if show_answers == True:
        problems = line1 + '\n' + line2 + '\n' + line3 + '\n' + line4
    
    else:
        problems = line1 + '\n' + line2 + '\n' + line3

    return problems

print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')

I’ve edited your code 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 (').

Make sure you are not adding too many spaces at the end of each row. (In fact there should be none).
You can double check by printing your output followed by some characters like “$$$” so you can try and see if there are extra spaces any where at the end.

I also suggest copying the test out and posting it here as we may be able to give you more information that way. (Don’t take a screenshot as the test output is better understood in text form so just copy and paste with your mouse). Finally please always include a link to the project in case someone needs to review it before responding to you first (this makes it easier for them, if you add a link)

You can also use repr() to see the output formatted as the tests indicate:

print(repr(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])))