Arithmetic Formatter

I’ve been trying my best to try to make the Arithmetic Formatter on my own, but my result of the final print call is not acceptable by it, and it is EXACTLY THE RESULT THEY ARE ASKING FOR. Please help to see what I can do to actually pass this Tests.

Here are the code, I think I pasted it correctly.
I have been working on it on Visual Studio, there are no exception, the formatting is clean, exactly as the example, instruction they are asking me. Screenshot included for more clarity. I even print the result they gave me out and I still unable to find a differences between my function print result vs their result.

My current code on GitHub (Currently trying to learning GitHub)

def arithmetic_arranger(problems, show_answer=False):
    sorting_problem = ''
    row1 = ''
    row2 = ''
    row3 = ''
    row4 = ''
    
    if len(problems) > 5:
        return "Error: Too many problems."
    
    for problem in problems:
        if '+' in problem:
            operator_index = problem.find('+')
            first_number = problem[:operator_index].strip()
            second_number = problem[operator_index + 1:].strip()
            operator = '+'
            if not first_number.isdigit() or not second_number.isdigit():
                return "Error: Numbers must only contain digits."
            if len(first_number) > 4 or len(second_number) > 4:
                return "Error: Numbers cannot be more than four digits."
        elif '-' in problem:
            operator_index = problem.find('-')
            first_number = problem[:operator_index].strip()
            second_number = problem[operator_index + 1:].strip()
            operator = '-'
            if not first_number.isdigit() or not second_number.isdigit():
                return "Error: Numbers must only contain digits."
            if len(first_number) > 4 or len(second_number) > 4:
                return "Error: Numbers cannot be more than four digits."
        else:
            return "Error: Operator must be '+' or '-'."
        
        # Construct the sorted problem string
        sorting_problem += f"{first_number.strip()} {operator} {second_number.strip()}\n"

        # Max length of the "----" row 3
        max_length = max(len(first_number), len(second_number))

        # Sorting the formats
        row1 += first_number.rjust(max_length + 2) + '    '
        row2 += operator + second_number.rjust(max_length + 1) + '    '
        row3 += '-' * (max_length + 2) + '    '

        # Calculate the result of the arithmetic if show_answer is True
        if show_answer == True:
            if operator == '+':
                result = str(int(first_number) + int(second_number))
            elif operator == '-':
                result = str(int(first_number) - int(second_number))
            row4 += result.rjust(5) + '    '
        if show_answer == False:
            row4 = ''
    
    # Combine the rows
    sorting_problem = row1.rstrip() + '\n' + row2.rstrip() + '\n' + row3.rstrip() + '\n' + row4.rstrip()
    
    # Return the arranged problems
    return sorting_problem

print(arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]))
#print(str("  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------."))

Please help me to understand how I could pass this course

you can use the repr() function to have the output of your function like this:

>>> '  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------\n'
>>> '  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------'

the first line is your output, the second line is the expected. You are almost there, it looks like you have an extra new line character at the end of your string

1 Like

Oh wow that repr() is something quite useful to get the comparison, thank you very much, with your help I was able to pass it. I will surely remember from now to check it side-by-side with code instead of with my actual eye.

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