I got the correct output but failed at test

Tell us what’s happening:

Your code so far

arrop = []
def arithmetic_arranger(problems, showans="False"):
    if(validate(problems) == True):
        for item in problems:
        #remove space
            prob = item.replace(" ", "")

            if( prob.count("+") == 1):
                x =  prob.split("+")
                x.insert(1, "+")
                arrop.extend(x)   
            else:
                x =  prob.split("-")
                x.insert(1, "-")
                arrop.extend(x)
        arranged_problems = output(arrop, showans)
    else:
        arranged_problems = validate(problems)
    return arranged_problems

def validate(arr):
    #check  if there are too many problem (max 5)
    if( len(arr) > 5): return "Error: Too many problems."
    
    #check if operand contain only number
    for item in arr:
        #remove space
        prob = item.replace(" ", "")

        #check if operator is Multiplication and division will return an error.
        if( prob.count("*") == 1 or prob.count("/") == 1 ): return "Error: Operator must be '+' or '-'."
            

        if( prob.count("+") == 1): 
            x =  prob.split("+")
        else:
            x = prob.split("-")
            
        for num in x:
            if(num.isnumeric() == False): return "Error: Numbers must only contain digits."
            if( int(num) > 10000 ): return  "Error: Numbers cannot be more than four digits."
    return True

def output(x, showans):
    ans = ""

    #count the number of dash needed
    arrlinlen = []

    #save the array of answer to be printed later
    arrans = []

    #print the first line or the left side operand
    for i in range (0, int( (len(x) / 3) ) ):
        operand1 = len(x[0 + 3 * i] )
        operand2 = len(x[2 + 3 * i] ) 

        if( operand1 < operand2 ):
            ans = ans + " " * (operand2 - operand1 + 1)
            ans = ans + " "
            ans = ans + x[0 + 3 * i]
            arrlinlen.append(operand2 + 1)
        else:
            ans = ans + "  " #change this later
            ans = ans+ x[0 + 3 * i]
            arrlinlen.append(operand1 + 1)

        strtemp = x[0 + 3 * i] + x[1 + 3 * i] + x[2 + 3 * i]
        arrans.append(eval(strtemp))

        if(i == int( (len(x) / 3) ) - 1):
            ans = ans + "\n"
        else:
            ans = ans + "    "

    #print the operator and the right side operand
    for i in range (0, int( (len(x) / 3) ) ):
        operand1 = len(x[0 + 3 * i] )
        operand2 = len(x[2 + 3 * i] ) 
        if( operand1 < operand2 ):
            ans = ans + x[1 + 3 * i] + " " + x[2 + 3 * i]
        else: 
            sp = operand1 - operand2
            ans = ans + x[1 + 3 * i] + (" " * (sp+1) ) +  x[2 + 3 * i]

        if(i == int( (len(x) / 3) ) - 1):
            ans = ans + "\n"
        else:
            ans = ans + "    "

    #print the dash
    for i in range (0, len(arrlinlen) ):
        ans = ans + "-" * (arrlinlen[i] + 1)

        if(i == len(arrlinlen) - 1 ):
            if(showans == True):
                ans = ans + "\n"
            ans = ans + ""
        else:
            ans = ans + "    "

    if(showans == True):
        for i in range (0, len(arrans) ):
            tmpspc = arrlinlen[i] - len(str(arrans[i])) 
            ans = ans + "  " * tmpspc + str(arrans[i])

            if(i == int( (len(x) / 3) ) - 1):
                ans = ans + ""
            else:
                ans = ans + "    "
    
    return ans


print( arithmetic_arranger( ["3 + 855", "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/85.0.4183.83 Safari/537.36.

Challenge: Arithmetic Formatter

Link to the challenge:

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

Thankyou very much! It Works perfectly. Didn’t realize that it could be a problem