Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I don’t know why but the 1-4 tests don’t work, even though I checked and they all follow the criteria set i think

Your code so far

def arithmetic_arranger(problems, show_answers=False):

    first_line = ''
    second_line = ''
    third_line = ''
    fourth_line = ''
    
    for problem in problems:
        num1, operand, num2 = problem.split()
        if len(problems) > 5:
            return ("Error: Too many problems.")
     
        elif operand == '*' or operand == '/':
            return ("Error: Operator must be '+' or '-'.")
      
        elif not num1.isdigit() or not num2.isdigit():
            return ('Error: Numbers must only contain digits.')
       
        elif len(num1) > 4 or len(num2) > 4:
            return ("Error: Numbers cannot be more than four digits.")
        
        width = max(len(num1), len(num2)) + 2

        if show_answers == True:
            if operand == '+':
                ans = int(num1) + int(num2)
            elif operand == '-':
                ans = int(num1) - int(num2)
            fourth_line += ' ' * (width - len(str(ans))) + str(ans) + '    '

        first_line += ' ' * (width - len(num1)) + num1 + '    '
        second_line += operand + ' ' * ((width - len(num2)) - 1) + num2 + '    '
        third_line += '-' * width + '    '

        line1 = first_line.rstrip(' ')
        line2 = second_line.rstrip(' ')
        line3 = third_line.rstrip(' ')
        line4 = fourth_line.rstrip(' ')

        formatted_problems = (line1 + '\n' + line2 + '\n' + line3 + '\n' + line4)

    return formatted_problems

print(f'{arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])}')

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

If you wrap the function call with repr, it will be easier to compare the printout with expected answer from tests. Ie.:

print(repr(arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])))
1 Like