I'm finding this waaay too hard

Tell us what’s happening:
I ran my code and it worked fine while I was testing locally, but when used the test module provided, my code didn’t pass a single test. I didn’t make the code with it giving more than two numbers per problem. Should I modify the code or start from scratch? but even if I did I have no other ideas on how to solve this…

Your code so far

def arithmetic_arranger(problems, Sum = None):
    # Preparation...
    ch_1 = list()
    ch_2 = list()
    ch_3 = list()
    ch_4 = list()
    ch_4_1 = list()
    problem_space = list()
    space_needed = list()
    space_between = list()
    ans_space = list()
    dump = list()

    # Too many problems detection code.
    if len(problems) > 5:
        return print("Error: Too many problems.")
        quit()

    # Separating into different lists.
    for problem in problems:
        nums = problem.split()
        ch_1.append(nums[0])
        ch_2.append(nums[1])
        ch_3.append(nums[2])

    # Wrong operator detection code.
    for opr in ch_2:
        # print(opr)
        if opr == "+" or opr == "-":
            continue
        else:
            return print("Error: Operator must be '+' or '-'.")
            quit()

    # Too many digits and wrong characters detection code.
    for dig in ch_1:
        # print(dig)
        try:
            dump.append(int(dig))
            if len(dig) > 4 :
                return print("Error: Numbers cannot be more than four digits.")
                quit()
        except:
            return print("Error: Numbers must only contain digits.")
            quit()

    for dig in ch_3:
        # print(dig)
        try:
            dump.append(int(dig))
            if len(dig) > 4 :
                return print("Error: Numbers cannot be more than four digits.")
                quit()
        except:
            return print("Error: Numbers must only contain digits.")
            quit()

    # Calculating spaces code, and constructing sums list code.
    for problem in problems:
        nums = problem.split()
        # print(nums)
        problem_space.append(max(len(nums[0]), len(nums[2])) + 2)
        # print(len(nums[0]), len(nums[2]))
        if nums[1] == "+":
            ch_4.append(int(nums[0]) + int(nums[2]))
        elif nums[1] == "-":
            ch_4.append(int(nums[0]) - int(nums[2]))
        if len(nums[0]) >= len(nums[2]):
            space_needed.append(6)
            space_between.append((abs(len(nums[2]) - len(nums[0]))) + 1)
        elif len(nums[0]) < len(nums[2]):
            space_needed.append((len(nums[2]) - len(nums[0])) + 6)
            space_between.append(1)
        if nums[1] == "+":
            cur_sum = str(int(nums[0]) + int(nums[2]))
        elif nums[1] == "-":
            cur_sum = str(int(nums[0]) - int(nums[2]))
        ans_space.append((max(len(nums[0]), len(nums[2])) + 2) - len(cur_sum))

    # Accounting for first arithmetic problem spacing.
    first = problems[0].split()
    if len(first[0]) >= len(first[2]):
        space_needed[0] = 2
    elif len(first[0]) < len(first[2]):
        space_needed[0] = (len(first[2]) - len(first[0])) + 2

    # Turning the sums list into strings.
    for num in ch_4:
        ch_4_1.append(str(num))

    # Debugging..
    # print(ch_1)
    # print(ch_2)
    # print(ch_3)
    # print(ch_4)
    # print(problem_space)
    # print(space_needed)
    # print(space_between)
    # print(ans_space)

    # Displaying the problems in a neat way.
    if len(problems) == 1:
        arranged_problems = " " * space_needed[0] + ch_1[0] + "\n" + ch_2[0] + " " * space_between[0] + ch_3[0] + "\n" + "-" * problem_space[0]
        if Sum == True:
            arranged_problems = arranged_problems + "\n" + " " * ans_space[0] + ch_4_1[0]
        return arranged_problems

    if len(problems) == 2:
        arranged_problems = " " * space_needed[0] + ch_1[0] + " " * space_needed[1] + ch_1[1] + "\n" + ch_2[0] + " " * space_between[0] + ch_3[0] + " " * 4 + ch_2[1] + " " * space_between[1] + ch_3[1] + "\n" + "-" * problem_space[0] + " " * 4 + "-" * problem_space[1]
        if Sum == True:
            arranged_problems = arranged_problems + "\n" + " " * ans_space[0] + ch_4_1[0] + " " * 4 + " " * ans_space[1] + ch_4_1[1]
        return arranged_problems

    if len(problems) == 3:
        arranged_problems = " " * space_needed[0] + ch_1[0] + " " * space_needed[1] + ch_1[1] + " " * space_needed[2] + ch_1[2] + "\n" + ch_2[0] + " " * space_between[0] + ch_3[0] + " " * 4 + ch_2[1] + " " * space_between[1] + ch_3[1] + " " * 4 + ch_2[2] + " " * space_between[2] + ch_3[2] + "\n" + "-" * problem_space[0] + " " * 4 + "-" * problem_space[1] + " " * 4 + "-" * problem_space[2]
        if Sum == True:
            arranged_problems = arranged_problems + "\n" + " " * ans_space[0] + ch_4_1[0] + " " * 4 + " " * ans_space[1] + ch_4_1[1] + " " * 4 + " " * ans_space[2] + ch_4_1[2]
        return arranged_problems

    if len(problems) == 4:
        arranged_problems = " " * space_needed[0] + ch_1[0] + " " * space_needed[1] + ch_1[1] + " " * space_needed[2] + ch_1[2] + " " * space_needed[3] + ch_1[3] + "\n" + ch_2[0] + " " * space_between[0] + ch_3[0] + " " * 4 + ch_2[1] + " " * space_between[1] + ch_3[1] + " " * 4 + ch_2[2] + " " * space_between[2] + ch_3[2] + " " * 4 + ch_2[3] + " " * space_between[3] + ch_3[3] + "\n" + "-" * problem_space[0] + " " * 4 + "-" * problem_space[1] + " " * 4 + "-" * problem_space[2] + " " * 4 + "-" * problem_space[3]
        if Sum == True:
            arranged_problems = arranged_problems + "\n" + " " * ans_space[0] + ch_4_1[0] + " " * 4 + " " * ans_space[1] + ch_4_1[1] + " " * 4 + " " * ans_space[2] + ch_4_1[2] + " " * 4 + " " * ans_space[3] + ch_4_1[3]
        return arranged_problems

    if len(problems) == 5:
        arranged_problems = " " * space_needed[0] + ch_1[0] + " " * space_needed[1] + ch_1[1] + " " * space_needed[2] + ch_1[2] + " " * space_needed[3] + ch_1[3] + " " * space_needed[4] + ch_1[4] + "\n" + ch_2[0] + " " * space_between[0] + ch_3[0] + " " * 4 + ch_2[1] + " " * space_between[1] + ch_3[1] + " " * 4 + ch_2[2] + " " * space_between[2] + ch_3[2] + " " * 4 + ch_2[3] + " " * space_between[3] + ch_3[3] + " " * 4 + ch_2[4] + " " * space_between[4] + ch_3[4] + "\n" + "-" * problem_space[0] + " " * 4 + "-" * problem_space[1] + " " * 4 + "-" * problem_space[2] + " " * 4 + "-" * problem_space[3] + " " * 4 + "-" * problem_space[4]
        if Sum == True:
            arranged_problems = arranged_problems + "\n" + " " * ans_space[0] + ch_4_1[0] + " " * 4 + " " * ans_space[1] + ch_4_1[1] + " " * 4 + " " * ans_space[2] + ch_4_1[2] + " " * 4 + " " * ans_space[3] + ch_4_1[3] + " " * 4 + " " * ans_space[4] + ch_4_1[4]
        return arranged_problems

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

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

Don’t get discouraged because you failed all the tests. You are very close to the solution already, so keep going!

Your code looks good so I’d recommend reworking it instead of starting from scratch.

The reason you are not passing the tests is one basic thing: Look at what the tests expect you to return.
Currently you are return print-functions, which I find kinda funny because I didn’t know you could do that and it would just work. It’s still going to fail tests because this actually returns None - the print function merely puts out the string but it doesn’t create a return-value.
Also return exits the function already, you don’t need a following quit().

So basically, remove the “print”-keyword in the returns and that might already bring you a lot closer to passing the tests.

I didn’t make the code with it giving more than two numbers per problem.

Yeah well read the assignment → because that’s exactly what you were supposed to do :wink:

1 Like

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

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