Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Preview looks correct, but code is still not passing the tests. Thought the issue was trailing white spaces, but even after removing those (I think I did at least) it’s still not passing. Not sure what the reason it isn’t passing is.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
#CHANGE THIS BACK TO FALSE BEFORE SUBMISSION
    line1 = ''
    line2 = ''
    line3 = '' 
    line4 = ''
    #the final strings to return at the end

    if len(problems) > 5:
        return "Error: Too many problems."
    #Handles problem limit error
    
    for problem in problems:
        problem_tuple = problem.split(" ")
        digit1 = problem_tuple[0]
        function = problem_tuple[1]
        digit2 = problem_tuple[2]
    #split and set variables

        if len(digit1) > 4 or len(digit2) > 4:
            return "Error: Numbers cannot be more than four digits."
        #handle length error
        if problem_tuple[1] == "/" or problem_tuple[1] == "*":
            return "Error: Operator must be '+' or '-'."
        #handles operator error

        if problem == problems[-1]:
            seperator = ""
        else:
            seperator = "     "
        #Spaces to seperate each column, sets to none if at the last problem
        
        
        width = max(len(digit1), len(digit2)) + 2
        #Show how much empty space is required to line up the results, if on the last problem set width to 0



        
        line1 += " " * (width - len(digit1)) + digit1 + seperator
        line2 += function + " " * (width - len(digit2) - 1) + digit2 + seperator
        line3 += ("-" * width) + seperator
        #Create first 3 lines

        if problem_tuple[1] == "+":
            result = int(problem_tuple[0]) + int(problem_tuple[2])
        if problem_tuple[1] == "-":
            result = int(problem_tuple[0]) - int(problem_tuple[2])
        line4 += (" " * (width - len(str(result)))) + str(result) + seperator
        #Create the 4th line, depending on the operator

    if show_answers:
        return line1 + "\n" + line2 + "\n" + line3 + "\n" + line4
    else:
        return line1 + "\n" + line2 + "\n" + line3



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/134.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

There is additional info in the devtools console (F12)

Use repr() to compare your output with the tests2. .

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

'  3801       123\n-    2     +  49\n------     -----'

3801 123\n- 2 + 49\n------ -----
3801 123\n- 2 + 49\n------ -----

‘’‘You need to edit your seperator variable to have one less whitespace when the current iteration of problems is not problems[-1] (When it’s not the last one)’‘’

if problem == problems[-1]:
seperator = “”
else:
seperator = " "

‘’‘Write a conditional statement to return the error ‘Error: Numbers must only contain digits.’ when there is more than one non numerical character in digit1 or digit2’‘’

if not digit1.isnumeric() or not digit2.isnumeric():
return ‘Error: Numbers must only contain digits.’