Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
When I use this code in my personal system, it works, perfectly and can pass all of the tests. But whenever I run “main.py”, there is always an AssertionError. Can you please help me with this?

Your code so far

import sys


def operation(p):
    a = p[0:p.find(" ")]
    b = p[(p.find(" ")+3):]
    strs = [a, b]

    nums = []
    for n in strs:
        nums.append(int(n))

    if "+" in p:
        nums.append(nums[0]+nums[1])
        nums.append("+")
    else:
        nums.append(nums[0]-nums[1])
        nums.append("-")

    return nums


def with_solution(p):
    nums = operation(p)
    arranged = """{:>10}
    {}{:>5}
    ------
    {:>6}""".format(
        nums[0], nums[3], nums[1], nums[2])

    return arranged


def without_solution(p):
    nums = operation(p)
    arranged = """{:>10}
    {}{:>5}
    ------""".format(
        nums[0], nums[3], nums[1])

    return arranged


def arithmetic_arranger(problems, solution=False):
    if len(problems) > 5:
        sys.exit("Error: Too many problems.")
    for problem in problems:
        if not (("+" in problem) or ("-" in problem)):
            sys.exit("Error: Operator must be '+' or '-'.")

        a = problem[0:problem.find(" ")]
        b = problem[(problem.find(" ")+3):]
        strs = [a, b]

        for num in strs:
            if len(num) > 4:
                sys.exit("Error: Numbers cannot be more than four digits.")

        nums = []
        for n in strs:
            try:
                nums.append(int(n))
            except:
                sys.exit("Error: Numbers must only contain digits.")

    strs = []
    if solution:
        for problem in problems:
            strs.append(with_solution(problem))
    else:
        for problem in problems:
            strs.append(without_solution(problem))

    if len(strs) == 1:
        arranged_problems = strs[0]
    else:
        lines = [strs[i].splitlines() for i in range(len(problems))]

        if len(problems) == 5:
            arranged_problems = '\n'.join(
                [v+w+x+y+z for v, w, x, y, z in zip(*lines)])
        elif len(problems) == 4:
            arranged_problems = '\n'.join(
                [w+x+y+z for w, x, y, z in zip(*lines)])
        elif len(problems) == 3:
            arranged_problems = '\n'.join([x+y+z for x, y, z in zip(*lines)])
        elif len(problems) == 2:
            arranged_problems = '\n'.join([y+z for y, z in zip(*lines)])

    return arranged_problems

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

Can you share the error? I suspect it is an issue with spacing.

Two things. First, you’re raising an exception via sys.exit() and not returning the error message per the specification. Second, you have a spacing issue:

         3      3801        45       123
    +  855    -    2    +   43    +   49
    ------    ------    ------    ------
======================================================================
    3      3801      45      123
+ 855    -    2    + 43    +  49
-----    ------    ----    -----

Your output is the first set and the second should be correct. You have extra leading spaces, indicated in the errors as + signs:

E         - - 698    - 3801    + 43    +  49    +  40
E         +     -  698    - 3801    +   43    +   49    +   40
E         ? ++++ +                   ++        +         +

The line with the initial - is the expected output and the line with the initial + is the actual output and the line with the initial ? is attempting to indicate the problems.

2 Likes

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