Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code is returning the correct error messages on the console when I run problems with incorrect digit length or with the wrong operator etc. However when I run the tests the four error message tests return as failing. I have checked character length, full stops, quotation marks etc and I am completely stumped. Any helped is much appreciated.

Your code so far

def arithmetic_arranger(problems, show_answers=False):

    #limit problems to 5.
    if len(problems) > 5:
        raise ValueError("'Error: Too many problems.'")

    #limit operators to + or - by splitting the list, search list for position 1
    #Then check if it is not + or -
    for problem in problems:
        operator = problem.split()
        if operator[1] not in ['+', '-']:
            raise ValueError("Error: Operator must be '+' or '-'")

    #ensure the operand is made of only digits
    for problem in problems:
        operand_digit_only = problem.split()
        if not (operand_digit_only[0].isdigit() and operand_digit_only[2].isdigit()):
            raise ValueError("Error: Numbers must only conatin digits.")
    for problem in problems:
        operand = problem.split()        
        if len(operand[0]) > 4:
            raise ValueError("Error: Numbers cannot be more than four digits.")
        if len(operand[2]) > 4:
            raise ValueError("Error: Numbers cannot be more than four digits.")

    #define list for each line
    top_line = []
    middle_line = []
    dash_line = []
    answer_line = []

    #give each list a value
    for problem in problems:
        parts = problem.split()
        first_operand = parts[0]
        operator = parts[1]
        second_operand = parts[2]
    
        #get the answer
        if operator == "+":
            answer = int(first_operand) + int(second_operand)
        elif operator == "-":
            answer = int(first_operand) - int(second_operand)
        
        #set the width of each problem
        width = max(len(first_operand), len(second_operand)) + 2

        top_line.append(first_operand.rjust(width))
        middle_line.append(operator + (" " + second_operand).rjust(width - 1)) 
        dash_line.append("-" * width)
        answer_line.append(str(answer).rjust(width))

    arranged_problems = "    ".join(top_line) + "\n" + "    ".join(middle_line) + "\n" + "    ".join(dash_line)

    if show_answers:
        arranged_problems += "\n" + "    ".join(answer_line)

    return arranged_problems

print(f'\n{arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])}')

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

I believe you are supposed to return the requested string instead of raising an error

Fantastic! You have solved all my problems.

1 Like