Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Code seems like it follows all the rules, but doesn’t pass about half the tests. I got it so all the error messages are good, as those just needed periods, but despite the output looking perfect otherwise it won’t count it as passing the tests.

image

Your code so far

def arithmetic_arranger(problems, show_answers=True):
    if len(problems) > 5:
        return('Error: Too many problems.')
#Lists of the first and second numbers in math problems
    first_number = []
    plus_or_minus = []
    second_number = []
    max_length = []
    for problem in problems:
#Splitting the first number, +/-, and second number into different lists
        problem_parts = problem.split()
        first_number.append(problem_parts[0])
        plus_or_minus.append(problem_parts[1])
        second_number.append(problem_parts[2])
        if problem_parts[1] == '*' or problem_parts[1] == '/':
            return("Error: Operator must be '+' or '-'.")
        if len(problem_parts[0]) > 4 or len(problem_parts[2]) > 4:
            return('Error: Numbers cannot be more than four digits.')
        if not (problem_parts[0].isdigit() and problem_parts[2].isdigit()):
            return('Error: Numbers must only contain digits.')
#Getting the length of the largest number in the problem so the problem has space
        problem_length = max(len(problem_parts[0]), len(problem_parts[2])) + 2
        max_length.append(problem_length)

#Making the lines of text for the math problem
    first_line = ''
    second_line = ''
    dashes_line = ''
    answer_line = ''

    for i in range(len(problems)):
        first_line += first_number[i].rjust(max_length[i]) + '    '
        second_line += plus_or_minus[i] + second_number[i].rjust(max_length[i] -1) + '    '

        dashes_line += '-' * max_length[i] + '    '

        if show_answers:
            answer = str(eval(problems[i]))
            answer_line += answer.rjust(max_length[i]) + '    '

#Put lines together
    rearranged_problem = first_line.rstrip() + '\n' + second_line.rstrip() + '\n' + dashes_line.rstrip()
    if show_answers:
        rearranged_problem += '\n' + answer_line.rstrip()

    print(rearranged_problem)
    return problems

print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

They only let me upload one image a post, so here’s what tests pass/fail for context:

Your function needs to return the formatted problems, it doesn’t matter what it prints. You could remove the print if you want, that’s more for troubleshooting.

Just changed that, still comes up with the same fails on the test unfortunately.

Try the tests out.

arithmetic_arranger(["3801 - 2", "123 + 49"]) should return   3801      123\n-    2    +  49\n------    -----

If I test your code:

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

Actually it did seem to help with two of the tests on second glance, although still having issues on the top four ones.

Do you see how the output of your code is different than what the test should return?

image
I tried printing what the first test was checking for, and it certainly does look different, although kinda bad. I doubt that was the intent of what the challenge was looking for as the numbers don’t line up at all.

I guess I could remove the right alignment from the top line to achieve these results, but I think the course creators might want to look into this.

Actually my bad, didn’t copy the extra spaces in front. Still leaves me at a loss though, I’ll try to cross examine it though and see if I can find any differences.

image
Example output of first test I failed. I really don’t see any difference.

(First is mine, second is the test it is cross checking)

When I test your code, it prints out the answers. Did you change that already?

You can also print using repr() which will show the raw output as the test indicates, it will be easier to analyze

print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"])))

I found out the the tests were mad because I changed the show answer flag to true for testing and it didn’t like that it was getting an answer in the output. It let me through now, thank you for helping me through this!

1 Like