Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Create an Arithmetic Formatter: Output looks correct, Tests raise unknown error?

As the title explains, I am able to see the desired output in the console for every test condition. I have also printed the final string while escaping the new line to make sure the string readout is exactly as it shows in the test solutions. However, whenever I run tests, ever test except two of the error handling tests show:

“Your code raised an error before any tests could run. Please fix it and try again.”

Again, the console preview throws zero errors.

I appreciate any clarity that can be granted, as my OCD will be bothered by not seeing the completion of this milestone project!

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    prohibited_chars = ['*', '/']
    allowed_chars = ['+', '-']
    split_operands = []
    problem_sets = []
    space = '    '
    #splitting the problems
    for _ in problems: 
        split_operands.append(_.split())

    #CHECKING ERRORS
    #check for more than 5 problems
    if len(problems) > 5: 
        print("Error: Too many problems.")
        return "Error: Too many problems."

    #check only Addition or substraction and only numbers
    for _ in range(len(split_operands)):
        for i in (split_operands[_]):
            #check for operands of more than 4 digits
            if len(i) > 4: 
                print("Error: Numbers cannot be more than four digits")
                return "Error: Numbers cannot be more than four digits"

            #check if operand is multiplication or div
            if i in prohibited_chars: 
                print("Error: Operator must be '+' or '-'.")
                return "Error: Operator must be '+' or '-'."

            #check if operand is not only digit
            if i.isdigit() == False and i not in allowed_chars:
                print("Error: Numbers must only contain digits")
                return "Error: Numbers must only contain digits"
            
    #expand lists to inlcude solution, spacing for readout, spacing reference, and  line drawing
    for _ in range(len(split_operands)):

        #generate solutions at index 3
        if split_operands[_][1] == '+':
            split_operands[_].append(str(int(split_operands[_][0]) + int(split_operands[_][2])))
        else:
            split_operands[_].append(str(int(split_operands[_][0]) - int(split_operands[_][2])))

        #determine spacing for readout at index 4
        split_operands[_].append((max(len(split_operands[_][0]),len(split_operands[_][2]))+2))

        #draw line index 5
        split_operands[_].append((max(len(split_operands[_][0]),len(split_operands[_][2]))+2) * '-')

        #re-create the operands to be the same equal length
        #first operand gets leading spaces
        split_operands[_][0] = ((split_operands[_][4]-len(split_operands[_][0]))*' ') + split_operands[_][0]

        #second Operand get's leading spaces
        split_operands[_][2] = ((split_operands[_][4]-len(split_operands[_][2]) - 1)*' ') + split_operands[_][2]
        #solutions get leading spaces
        split_operands[_][3] = ((split_operands[_][4]-len(split_operands[_][3]))*' ') + split_operands[_][3]
    #Create each of the strings that will make up the printout
    line1 = ''
    line2 = '' 
    line3 = ''
    line4 = ''
    
    for _ in range(len(split_operands)):
        #creates first operand
        line1 += (split_operands[_][0] + space) 

        #creates second operand with +or -
        line2 += (split_operands[_][1] + split_operands[_][2] + space)

        #creates line
        line3 += (split_operands[_][5] + space)
        #creats solution
        line4 += (split_operands[_][3] + space)
    
    linelist = [line1, line2, line3, line4]

    #Print out problems
    print_order = 4 if show_answers else 3 #checking to see if answers will be shown
    problems = ''
    for y in range(print_order):
        problems += linelist[y] +'\n'
    #print(problems)

    

    return problems


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

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

there has been an issue with one update recently

It should show the message as the hints on the left, instead when a test fails it is showing “Your code raised an error before any tests could run. Please fix it and try again.”

You can check your output in more details using the repr() function

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

this for example prints

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

and if you confront that with the string written in the test

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

you have some differences at the end of each line

the other way to check is opening the browser console, there is a more detailed output from the tests

thank you.

I made my space variable “~~~~” to help me visualize.

I had to delete the tail spaces on each line and delete the final /n. Finally got it to work.

Do you have an image of what i should see in the console view? I hit F12 as suggested, but it just seemed to be a lot of javascript at first glance

EDIT: Disregard. I saw that I needed to navigate over to console.

1 Like