Passing all but one test - Build an Arithmetic Formatter Project

Tell us what’s happening:

Been working on the arithmetic formatter project for some time now, and before I was having problems with passing most of the tests and realized I had misspelling one of the variables within my code and now im not passing one of the tests and I believe it is because the code that I am returning is incorrect but im not entirely sure on what I’m supposed to change it to as it doesn’t result in the test passing when I do change it and I’m not sure on what all to do next, more specifally im getting the " arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"]) should return 'Error: Numbers cannot be more than four digits.'" test failing consistently.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
#1 Check the length of the parameter
    if len(problems) > 5:
        return "Error: Too many problems."

#2 Check the operand
    operators = []
    for problem in problems:
        array = problem.split()
        operators.append(array[1])

    for operator in operators:
        if operator in ['*', '/']:
            return "Error: Operator must be '+' or '-'."
    
#3 Check for non-digits
    numbers = []
    for problem in problems:
        array = problem.split()
        numbers.append(array[0])
        numbers.append(array[2])
    
    for number in numbers:
        if not number.isdigit():
            return 'Error: Numbers must only contain digits.'
#4 Check Operand Length        
        if len(number) > 4:
            return "Error Numbers cannot be more than four digits."

#5 Evaluation
    answers = []
    top_row = ''
    bottom_row = ''
    answer_row = ''
    dashes = ''

    for i in range(0, len(numbers), 2):
        num1 = int(numbers[i])
        num2 = int(numbers[i + 1])
        operator = operators[i // 2]

        if operator == '+':
            result = num1 + num2
        else:
            result = num1 - num2
        answers.append(result)

    

#6 Formatting problem rows
        space_width = max(len(numbers[i]), len(numbers[i + 1])) + 2
        top_row += numbers[i].rjust(space_width)
        bottom_row += operator + numbers[i + 1].rjust(space_width - 1)
        dashes += '-' * space_width

#7 Spacing between problems
        if i != len(numbers) - 2:
            top_row += ' ' * 4
            bottom_row +=  ' ' * 4
            dashes +=  ' ' * 4
    
#8 Formatting answers row
    for i in range(len(answers)):
        space_width = max(len(numbers[2 * i]), len(numbers[2 * i + 1])) + 2
        answer_row += str(answers[i]).rjust(space_width)

#9 Spacing between answers
        if i != len(answers) - 1:
            answer_row += ' ' * 4

#10 Final Arrangement and return
    if show_answers:
        arranged_problems = '\n'.join((top_row, bottom_row, dashes, answer_row))
    else:
        arranged_problems = '\n'.join((top_row, bottom_row, dashes,))


        print(arranged_problems)
    
    return (arranged_problems)

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

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project
https://www.freecodecamp.org/learn/scientific-computing-with-python/build-an-arithmetic-formatter-project/build-an-arithmetic-formatter-project`Preformatted text`

There’s just a little detail missing - colon after the Error.

2 Likes

Welp that did it! Thanks man!

1 Like