Project Assignment - Arithmetic Formatter

Tell us what’s happening:
Describe your issue in detail here.
While running the program or code that I created for this assignment, I am getting 6 assertion errors as per the test_module.py.

The errors with the following ids are displayed.

  1. test_two_problems_arrangement1
  2. test_two_problems_arrangement2
  3. test_four_problems_arrangement
  4. test_five_problems_arrangement
  5. test_two_problems_with_solutions
  6. test_five_problems_with_solutions

I really can’t make out what exactly these errors are pointing out. It could be the formatting but without knowing specifically what, I don’t know where to make the changes. As far as the spacing requirements (btw the values and problems) are concerned, I believe it has been taken care of. Please advise!

Your code so far

def arithmetic_arranger(myinput, showresult=False) :
    myOperand1 = []
    myOperand2 = []
    myOpr = []
    count = 0

    for inputs in myinput:
        myList = inputs.split(" ")
        count += 1
        try:
            if isinstance(int(myList[0]), int) & isinstance(int(myList[2]), int):
                myOperand1.append(myList[0])
                myOperand2.append(myList[2])
        except ValueError:
            return "Error: Numbers must only contain digits."
        if myList[1] == "+" or myList[1] == "-":
            myOpr.append((myList[1]))
        else:
            return "Error: Operator must be '+' or '-'."
        if count > 5:
            return "Error: Too many problems."
        if len(myList[0]) > 4 or len(myList[2]) > 4:
            return "Error: Numbers cannot be more than four digits."


    # print all the first operands with the required spacing
    for i in range(len(myOperand1)):
        if len(myOperand1[i]) <= len(myOperand2[i]):
            print(myOperand1[i].rjust(len(myOperand2[i]) + 2) + "    ",end=" ")
        else:
            print(myOperand1[i].rjust(len(myOperand1[i]) + 2) + "    ", end=" ")
    print()

    # print the operators and the second operands with the required spacing
    for i in range(len(myOperand2)):
        if len(myOperand1[i]) <= len(myOperand2[i]):
            print(myOpr[i] + " " + myOperand2[i].rjust(len(myOperand2[i])) + "    ",end=" ")
        else:
            print(myOpr[i] + " " + myOperand2[i].rjust(len(myOperand1[i])) + "    ", end=" ")

    print()

    # print the dash ----
    for i in range(len(myOperand2)):
        if len(myOperand1[i]) <= len(myOperand2[i]):
            num = len(myOperand2[i]) + 2
            print(num * "-" + "    ",end=" ")
        else:
            num = len(myOperand1[i]) + 2
            print(num * "-" + "    ", end=" ")

    print()

    # print the sum or difference btw the operands as per the operator
    if showresult:
        for i in range(len(myOperand2)):
            if myOpr[i] == "+":
                sum = int(myOperand1[i]) + int(myOperand2[i])
                if len(myOperand1[i]) <= len(myOperand2[i]):
                    print(str(sum).rjust(len(myOperand2[i]) + 2) + "    ", end=" ")
                else:
                    print(str(sum).rjust(len(myOperand1[i]) + 2) + "    ", end=" ")
            else:
                sum = int(myOperand1[i]) - int(myOperand2[i])
                if len(myOperand1[i]) <= len(myOperand2[i]):
                    print(str(sum).rjust(len(myOperand2[i]) + 2) + "    ", end=" ")
                else:
                    print(str(sum).rjust(len(myOperand1[i]) + 2) + "    ", end=" ")

    return "Completed"

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36

Challenge: Arithmetic Formatter

Link to the challenge:

It would help if you provide the error messages.

You shouldn’t use print anywhere in your code.

Thank you for replying. Are you sure that I shouldn’t be using print anywhere in the function to output the results. Wish FCC had clearly mentioned about it in the requirements.

As such, using print (and all the same code) is giving me the correct formatted results when i tried the code separately on my IDE.

I’m very sure that you should not be using print. The instructions say to return a string containing the output.

1 Like

Okay Jeremy, really appreciate your feedback. I am going to try that way and revert back.

Jeremy, as you did suggest, I removed all the print statements and returned the string. But the error stays.

What is ‘the string’ that you are returning?

def arithmetic_arranger(myinput, showresult=False):
    
    myOperand1 = []
    myOperand2 = []
    myOpr = []
    count = 0
    outputStr = ""

    for inputs in myinput:
        myList = inputs.split(" ")
        count += 1
        try:
            if isinstance(int(myList[0]), int) & isinstance(
                    int(myList[2]), int):
                myOperand1.append(myList[0])
                myOperand2.append(myList[2])
        except ValueError:
            return "Error: Numbers must only contain digits."
        if myList[1] == "+" or myList[1] == "-":
            myOpr.append((myList[1]))
        else:
            return "Error: Operator must be '+' or '-'."
        if count > 5:
            return "Error: Too many problems."
        if len(myList[0]) > 4 or len(myList[2]) > 4:
            return "Error: Numbers cannot be more than four digits."
    

    # Save all the first operands with the required spacing in a string
    for i in range(len(myOperand1)):
        if len(myOperand1[i]) <= len(myOperand2[i]):
            outputStr += myOperand1[i].rjust(len(myOperand2[i]) + 2) + "    "
        else:
            outputStr += myOperand1[i].rjust(len(myOperand1[i]) + 2) + "    "
    outputStr += "\n"

    # save the operators and the second operands with the required spacing in to the string
    for i in range(len(myOperand2)):
        if len(myOperand1[i]) <= len(myOperand2[i]):
            outputStr += myOpr[i] + " " + myOperand2[i].rjust(
                len(myOperand2[i])) + "    "
        else:
            outputStr += myOpr[i] + " " + myOperand2[i].rjust(
                len(myOperand1[i])) + "    "

    outputStr += "\n"

    # print the dashes ----
    for i in range(len(myOperand2)):
        if len(myOperand1[i]) <= len(myOperand2[i]):
            num = len(myOperand2[i]) + 2
            outputStr += num * "-" + "    "
        else:
            num = len(myOperand1[i]) + 2
            outputStr += num * "-" + "    "

    outputStr += "\n"

    # print the sum or difference btw the operands as per the operator
    if showresult:
        for i in range(len(myOperand2)):
            if myOpr[i] == "+":
                sum = int(myOperand1[i]) + int(myOperand2[i])
                if len(myOperand1[i]) <= len(myOperand2[i]):
                    outputStr += str(sum).rjust(len(myOperand2[i]) +
                                                2) + "    "
                else:
                    outputStr += str(sum).rjust(len(myOperand1[i]) +
                                                2) + "    "
            else:
                sum = int(myOperand1[i]) - int(myOperand2[i])
                if len(myOperand1[i]) <= len(myOperand2[i]):
                    outputStr += str(sum).rjust(len(myOperand2[i]) +
                                                2) + "    "
                else:
                    outputStr += str(sum).rjust(len(myOperand1[i]) +
                                                2) + "    "
return outputStr

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

It looks like you are always adding extra spaces to the end of each line.

If those extra spaces were there, it would have shown in the output.
The output is displayed in the required format. That is why I’m confused.

wish there was a way to share the screenshots. I could have send you how the screenshot of the output.

Spaces are hard to see.

Did you see the extra 10 spaces I put at the end of that previous line? You are adding 4 extra spaces to the end of each line.

It also doesn’t look like you are creating a multi line output with each result side by side? Oh, you have many loops. You still have extra spaces though.

Providing a link to your repl makes this much easier.

As per the requirement " There should be four spaces between each problem". This is why I specifically added 4 spaces after every operand.

The way I have created this, I am saving all the first operands in the string first with required spaces and then the operator and all the second operands.

sure, i can share the link and I hope it’s ok to do so.

https://replit.com/@RobinGeorge1/boilerplate-arithmetic-formatter#main.py

Between not after.

You have:
problem1 spaces problem2 spaces

You should have:
problem1 spaces problem2

1 Like

boilerplate-arithmetic-formatter - Replit

Yep, the diff says that you have 4 extra spaces at the end of each line.

That was a valid point from you about having spaces inbtw problems and not after. I can check on that but it is not affecting the output though.

It is affecting the output… The extra spaces are wrong. Since the extra spaces are not part of the specification, your string fails to match the expected output.