Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Hi,
I finished the Arithmetic Formatter test but somehow I don’t understand why it’s not passing the test ? The output is identical to what’s asked …

Your code so far

def arithmetic_arranger(problems, show_solutions=False):

    line1 = ""
    line2 = ""
    line3 = ""
    line4 = ""

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

    operations = {"+": lambda a, b: a + b, "-": lambda a, b: a - b}

    for problem in problems:
        parts = problem.split(" ")
        if parts[1] not in operations:
            return "Error: Operator must be '+' or '-'."
        if not parts[0].isnumeric() or not parts[2].isnumeric():
            return "Error: Numbers must only contain digits."
        if len(parts[0]) > 4 or len(parts[2]) > 4:
            return "Error: Numbers cannot be more than four digits."

        num1 = parts[0]
        operator = parts[1]
        num2 = parts[2]

        hyphen = "-" * (max(len(num1), len(num2)) + 2)
        line1 += "{:>6}".format(num1) + "    "

        if len(num1) >= len(num2):
            line2 += "{:>6}".format(operator + " " * (len(num1) - len(num2) + 1) + num2) + "    "
        else:
            line2 += "{:>6}".format(operator + " " + num2) + "    "

        line4 += "{:>6}".format(operations[operator](int(num1), int(num2))) + "    "
        line3 += "{:>6}".format(hyphen) + "    "

    lines = [line1, line2, line3]
    if show_solutions:
        lines.append(line4)
    return "\n".join(line.rstrip() for line in lines)

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

What does the test suite say? The diff output cat be hard to read, but it usually helps ID the problem.

For example for the first test :

_______________ test_template[test_two_problems_arrangement1] _______________

arguments = [['3801 - 2', '123 + 49']]
expected_output = '  3801      123\n-    2    +  49\n------    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801       123\n-    2     +  49\n------     -----' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801       123
E         ?             +
E         - -    2    +  49
E         + -    2     +  49
E         ?           +
E         - ------    -----
E         + ------     -----
E         ?           +

test_module.py:77: AssertionError

And when I just print out this specific test this is what I have :

  3801       123
-    2     +  49
------     -----
  3799       172

I mean it’s printing correctly on the console.

-   3801      123
+   3801       123
?             +
- -    2    +  49
+ -    2     +  49
?           +
- ------    -----
+ ------     -----
?           +

This is showing spacing issues in your output

Oh okay this actually shows where the mistakes are. I didn’t know about this.

Its a shame that Repl.it doesn’t show the colorized output