Arithmetic Formatter...So Close Please Help!

My seems correct but only passes four of the tests. What’s wrong?

def arithmetic_arranger(problems, solve = False):
    top = ""
    bottom = ""
    operator = ""
    dashes = ""
    answer = ""
    first_line = ""
    second_line = ""

    if len(problems) >= 5:
        return "Error: Too many problems."

    for problem in problems:
        top = problem.split(" ")[0]
        if not top.isdigit():
            return "Error: Numbers must only contain digits."
        if int(top) > 9999:
            return "Error: Numbers cannot be more than four digits."

        bottom = problem.split(" ")[2]
        if not bottom.isdigit():
            return "Error: Numbers must only contain digits."
        if int(bottom) > 9999:
            return "Error: Numbers cannot be more than four digits."

        operator = problem.split(" ")[1]

        if operator != "+" or "-":
            return "Error: Operator must be '+' or '-'."

        for problem in problems:
            top = problem.split(" ")[0]
            operator = problem.split(" ")[1]
            bottom = problem.split(" ")[2]
            Max = max(len(top), len(bottom)) + 2

        if problem != problems[-1]:
            first_line += top.rjust(Max) + "    "
            second_line += operator + " " + bottom.rjust(Max - 2) + "    "
            dashes += "-" * Max + "    "
        else:
            first_line += top.rjust(Max)
            second_line += operator + " " + bottom.rjust(Max - 2)
            dashes += "-" * Max

        if operator == "+":
            answer += str(int(top) + int(bottom)).rjust(Max) + "    "
        else:
            answer += str(int(top) - int(bottom)).rjust(Max) + "    "

        print(first_line)
        print(second_line)
        print(dashes)
        print(answer)

You shouldn’t be printing the output. You need to make it into a string and return it.

2 Likes

You’ll run into at least two additional issues apart from what @JeremyLT said:
First you are not using the solve argument.
Second you will have excess spaces at the end of your lines.

2 Likes

This is inside of the indentation level of the for loop, so you will return after one problem

Here you are always adding a space to the end of the line, no matter what…

Same here

1 Like

Thanks @JeremyLT @Jagaya . Are you saying to remove the spaces completely at the end of “answer” and “dashes”? Also, I fixed the indentation level of “while solve:” so that it’s outside of the for loop. My code is still not passing the tests though. Still can’t figure out what’s wrong.

def arithmetic_arranger(problems, solve = False):
    top = ""
    bottom = ""
    operator = ""
    dashes = ""
    answer = ""
    first_line = ""
    second_line = ""

    if len(problems) >= 5:
        return "Error: Too many problems."

    for problem in problems:
        top = problem.split(" ")[0]
        if not top.isdigit():
            return "Error: Numbers must only contain digits."
        if int(top) > 9999:
            return "Error: Numbers cannot be more than four digits."

        bottom = problem.split(" ")[2]
        if not bottom.isdigit():
            return "Error: Numbers must only contain digits."
        if int(bottom) > 9999:
            return "Error: Numbers cannot be more than four digits."

        operator = problem.split(" ")[1]

        if operator != "+" or "-":
            return "Error: Operator must be '+' or '-'."

    for problem in problems:
        top = problem.split(" ")[0]
        operator = problem.split(" ")[1]
        bottom = problem.split(" ")[2]
        Max = max(len(top), len(bottom)) + 2

        if problem != problems[-1]:
            first_line += top.rjust(Max) + "    "
            second_line += operator + " " + bottom.rjust(Max - 2) + "    "
            dashes += "-" * Max + "    "
        else:
            first_line += top.rjust(Max)
            second_line += operator + " " + bottom.rjust(Max - 2) + "    "
            dashes += "-" * Max + " "

    while solve:
        if operator == "+":
            answer += str(int(top) + int(bottom)).rjust(Max) + " "
            return first_line, "\n", second_line, "\n", dashes, "\n", answer
        else:
            answer += str(int(top) - int(bottom)).rjust(Max) + " "
            return first_line, "\n", second_line, "\n", dashes```

It’s easier if you provide a link to the repl.

A return statement still immediately stops your function. You still have to take care of that problem…

You’re adding extra spaces to the second and third line here.

I’m not sure, but I doubt commas concatenate strings in Python.

1 Like

Thank you @JeremyLT! Here’s the link to my Replit. I’m still struggling to make it works so any help is appreciated.

https://replit.com/@acrow/boilerplate-arithmetic-formatter#arithmetic_arranger.py

There is no link, please check again - it may fell victim to the forums auto-format.

Also as @JeremyLT already hinted at, you don’t concatenate strings with commas. Python interpets commas as part of a tuple. Strings get concatenated with “+”.

@Jagaya link is there below the post. I replaced commas with plus signs, but return statement is still not working.

This is still a problem… If you have a return statement inside of your loop, you will only loop once.

+ Error: Operator must be '+' or '-'.
-   3801      123
- -    2    +  49
- ------    -----

All errors look like this: You are returning this error message.
And the reason is quite simple
Your condition is:
if operator != "+" or "-":
The “or” is executed AFTER the left and right statements are calculated. So you are checking if “operator != ‘+’” is true or “’-’” is true. And by definition a non-empty string is always True → hence this condition is always True → hence it always returns the error message.

Please look again on how to properly combine logical statements in code.

@Jagaya @JeremyLT I have updated my replit. Do the changes I’ve made to operator and return statements look correct?

solution = first_line + "\n" + second_line + "\n" + dashes + "\n"

Everything is correct EXCEPT the linebreak at the end, if the solution is not to be shown.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.